下载地址http://code.google.com/p/crypto-js/
js代码
<script src="hmac-sha1.js"></script> <script> var hash = CryptoJS.HmacSHA1("Message","d1419c25a711ed6725429a85a9ed951b"); document.write(hash); </script>
返回结果是先经过SHA1加密,然后再16进制编码
对应Java代码
public static String encode(String data, String key) {
byte[] byteHMAC = null;
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
mac.init(spec);
byteHMAC = mac.doFinal(data.getBytes());
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return Hex.encodeHexString(byteHMAC);
}
System.err.println(encode("Message","d1419c25a711ed6725429a85a9ed951b"));
转载于:https://blog.51cto.com/xwebos/1217345