下载地址http://code.google.com/p/crypto-js/
js代码
var hash = CryptoJS.HmacSHA1(“Message”,”d1419c25a711eda85a9ed951b”);
document.write(hash);
返回结果是先经过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”,”d1419c25a711eda85a9ed951b”));
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/209331.html原文链接:https://javaforall.net
