加簽數據庫
驗籤性能
public static String sign(String text, String salt, String inputCharset) { return DigestUtils.md5Hex(getContentBytes(text + salt, inputCharset)); } public static boolean verify(String text, String sign, String salt, String inputCharset) { String mysign = DigestUtils.md5Hex(getContentBytes(text + salt, inputCharset)); return mysign.equals(sign); } private static byte[] getContentBytes(String content, String charset) { if (charset == null || "".equals(charset)) { return content.getBytes(); } try { return content.getBytes(charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException("MD5簽名過程當中出現錯誤,指定的編碼集不對,您目前指定的編碼集是:" + charset); } }
執行100萬組加驗籤操做,本機用時1400ms。也就是平均每組加驗籤用時0.0014ms(粗略估計)。測試
public static void main(String[] args) { FinanceAccount fa = new FinanceAccount(); fa.setBalance(BigDecimal.TEN); fa.setLocking(BigDecimal.TEN); String salt = "dskfnweabghcxoihvwei"; String signature = SecurityUtil.sign(fa.toSignString(), salt, null); int i = 0; long start = System.currentTimeMillis(); while (++i < 1000000) if (SecurityUtil.verify(fa.toSignString(), signature, salt, null)) { SecurityUtil.sign(fa.toSignString(), salt, null); } else { System.out.println("verification fail!"); } } System.out.println((System.currentTimeMillis() - start)); }