HMAC-SHA1:如何在Java中正确地实现?

61

我正在使用Java用HMAC-SHA1算法对一些值进行哈希,使用以下代码:

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实现返回的值。

因此第一次尝试是:

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

该函数返回:74ae5a4a3d9996d5918defc2c3d475471bbf59ac

我的Java函数也返回74ae5a4a3d9996d5918defc2c3d475471bbf59ac

好的,看起来它正常工作。然后我尝试使用一个更复杂的密钥:

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

我的PHP代码返回的哈希值为:e98bcc5c5be6f11dc582ae55f520d1ec4ae29f7a

而这次我的Java实现返回的是:c19fccf57c613f1868dd22d586f9571cf6412cd0

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

有什么提示吗?


5
PHP中的$符号会被解释为变量吗? - Alex Gitelman
你为什么需要十六进制相位? - AlikElzin-kilaka
我有类似的问题,无法解决它。https://stackoverflow.com/questions/60971203/how-to-compute-hmac-on-the-bases-of-payload-and-secret-key - A.R
5个回答

52

在您的PHP代码中,使用单引号将键值括起来,以避免$字符被视为变量引用。例如:

在您的PHP端,使用单引号将键括起来,这样$字符就不会被视为变量引用了。

hash_hmac("sha1", "helloworld", 'PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo')
否则,您实际获得的密钥是 PRIE7-Yf17kEnUEpi5hvW/#AFo(假设变量$oG2uS未定义)。

18

推荐使用Apache Common Codec Library,这个库非常简单易用。可以使用以下代码进行HmacSha1加密:HmacUtils.hmacSha1Hex(key, string_to_sign);


10
不再使用。现在请改用以下方式:new HmacUtils(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(content的输入流)。 - wild_nothing

9
其他回答指出的使用 Apache commons 的 HmacUtils 现已过时。Apache commons 现在建议使用以下方法: new HmacUtils(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(string_to_sign)

7

在 PHP 中,双引号 ("") 中的任何 $ 符号都被视为变量。您可以通过使用单引号(如上一个评论者所指出的)或通过以下方式转义美元符号来避免错误

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

注意:$现在应该改为\$。


2
在Java中,使用maven:
将以下依赖项添加到pom.xml中:
 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.4</version>
    </dependency>

然后尝试使用这个进行签名

HmacUtils.hmacSha1Hex(key, string_to_sign);

hmacSha1Hex已被弃用,请更新您的答案。 - Rajat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接