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

Posted

技术标签:

【中文标题】HMAC-SHA1:如何在 Java 中正确执行?【英文标题】:HMAC-SHA1: How to do it properly in Java? 【发布时间】:2011-09-12 20:13:02 【问题描述】:

我正在使用 HMAC-SHA1 散列一些值,使用 Java 中的以下代码:

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

返回:e98bcc5c5be6f11dc582ae55f520d1ec4ae29f7a

这一次我的 Java 实现返回:c19fccf57c613f1868dd22d586f9571cf6412cd0

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

有什么建议吗?

【问题讨论】:

它不会将 PHP 中的 $ 字符解释为变量吗? 为什么需要十六进制阶段? 我有同样的问题,无法解决***.com/questions/60971203/… 【参考方案1】:

在您的 PHP 端,在键周围使用单引号,这样$ 字符就不会被视为变量引用。即,

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

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

【讨论】:

【参考方案2】:

推荐Apache Common Codec Library,相当简单好用。 HmacUtils.hmacSha1Hex(key, string_to_sign);

【讨论】:

已弃用。现在改用这个: new HmacUtils(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(inputStreamOfContent);【参考方案3】:

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

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

注意 $ 现在是 \$

【讨论】:

【参考方案4】:

从 Apache commons 中指出 HmacUtils 的其他答案现在已被弃用。 Apache commons 现在建议使用:

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

【讨论】:

【参考方案5】:

Java 中,并使用 ma​​ven

将以下依赖添加到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 已弃用,请更新您的答案

以上是关于HMAC-SHA1:如何在 Java 中正确执行?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 TwitterOAuth (abraham/twitteroauth) 在 PHP 中为 Twitter 获取/生成 HMAC-SHA1 签名

对于 PBKDF2,使用 HMAC-SHA256 是不是比使用 HMAC-SHA-1 更安全?

在 python 中实现 HMAC-SHA1

如何使用Node.js Crypto创建HMAC-SHA1哈希?

如何在 Java 中正确执行线程回调?

用于在 Android 上生成 HMAC-SHA1 OAuth 签名的库?