[英]hash_hmac analog for Java
我需要在Java中获取字符串的哈希值,这与PHP的hash_hmac相同。
这是我的PHP示例:
$secret_key = 'foo';
$sig_str = 'bar';
$hash = hash_hmac('md5', $sig_str, $secret_key);
这就是我为Java找到的:
SecretKeySpec keySpec = new SecretKeySpec("foo".getBytes(), "HmacMD5");
Mac mac = Mac.getInstance(keySpec.getAlgorithm());
mac.init(keySpec);
String result = new BASE64Encoder().encode(mac.doFinal("bar".getBytes()));
但是结果是不同的。 我想念什么?
在这里找到解决方案: 相当于php的hmac-SHA1的java
问题出在BASE64Encoder中。 采用
for (byte b : digest) {
System.out.format("%02x", b);
}
System.out.println();
要么
new BigInteger(digest).toString(16);
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.