关于hash:HMAC-SHA1:如何在Java中正确执行?

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);
    }
}

Hex()属于org.apache.commons.codec

在PHP中,有一个类似的函数hash_hmac(algorithm, data, key),我可以用来比较Java实现返回的值。

因此,首先尝试是:

1
hash_hmac("sha1","helloworld","mykey") // PHP

返回:74ae5a4a3d9996d5918defc2c3d475471bbf59ac

我的Java函数也返回74ae5a4a3d9996d5918defc2c3d475471bbf59ac

好的,看来可行。 然后,我尝试使用一个更复杂的键:

1
hash_hmac("sha1","helloworld","PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo") // PHP

返回:e98bcc5c5be6f11dc582ae55f520d1ec4ae29f7a

这次,我的Java展示返回:c19fccf57c613f1868dd22d586f9571cf6412cd0

我的PHP代码返回的哈希值不等于我的Java函数返回的值,而且我找不到原因。

有小费吗?


在PHP方面,请在键周围使用单引号,以使$字符不被视为变量引用。 即

1
hash_hmac("sha1","helloworld", 'PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo')

否则,您真正得到的键是PRIE7-Yf17kEnUEpi5hvW/#AFo(假设未定义变量$oG2uS)。


推荐Apache通用编解码器库,它非常简单易用。
HmacUtils.hmacSha1Hex(key, string_to_sign);


双引号(")中的任何$符号都被视为PHP中的变量。您可以通过使用前一个注释者指出的单引号来避免错误,也可以按如下所示转义美元符号

1
hash_hmac("sha1","helloworld","PRIE7\\$oG2uS-Yf17kEnUEpi5hvW/#AFo")

注意$现在是\ $


到现在为止,已经不推荐使用其他来自Apache Commons的HmacUtils答案。 现在,Apache Commons建议使用:

new HmacUtils(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(string_to_sign)


在Java中,并使用maven:

将以下依赖项添加到pom.xml中:

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);