HMAC-SHA1: How to do it properly in Java?
我正在使用HMAC-SHA1,在Java中使用以下代码对一些值进行哈希处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public static String hmacSha1(String value, String key) { try { // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes,"HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes,"UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } |
在PHP中,有一个类似的函数
因此,首先尝试是:
1 | hash_hmac("sha1","helloworld","mykey") // PHP |
返回:
我的Java函数也返回
好的,看来可行。 然后,我尝试使用一个更复杂的键:
1 | hash_hmac("sha1","helloworld","PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo") // PHP |
返回:
这次,我的Java展示返回:
我的PHP代码返回的哈希值不等于我的Java函数返回的值,而且我找不到原因。
有小费吗?
在PHP方面,请在键周围使用单引号,以使
1 | hash_hmac("sha1","helloworld", 'PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo') |
否则,您真正得到的键是
推荐Apache通用编解码器库,它非常简单易用。
双引号(")中的任何$符号都被视为PHP中的变量。您可以通过使用前一个注释者指出的单引号来避免错误,也可以按如下所示转义美元符号
1 | hash_hmac("sha1","helloworld","PRIE7\\$oG2uS-Yf17kEnUEpi5hvW/#AFo") |
注意$现在是\ $
到现在为止,已经不推荐使用其他来自Apache Commons的HmacUtils答案。 现在,Apache Commons建议使用:
在Java中,并使用maven:
将以下依赖项添加到
1 2 3 4 5 6 | <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> commons-codec</artifactId> <version>1.4</version> </dependency> |
然后尝试使用此签名
1 | HmacUtils.hmacSha1Hex(key, string_to_sign); |