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

发布于 2024-11-15 02:01:15 字数 1580 浏览 4 评论 0原文

我使用 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 impl返回:c19fccf57c613f1868dd22d586f9571cf6412cd0

我的PHP代码返回的散列不等于值由我的 Java 函数返回,我不知道为什么。

有什么建议吗?

I'm hashing some values using HMAC-SHA1, using the following code in 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() belongs to org.apache.commons.codec

In PHP there's a similar function hash_hmac(algorithm, data, key) that I use to compare the values returned by my Java implementation.

So the first try is:

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

that returns: 74ae5a4a3d9996d5918defc2c3d475471bbf59ac

My Java function returns 74ae5a4a3d9996d5918defc2c3d475471bbf59ac as well.

Ok, it seems working. Then I try to use a more complex key:

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

that returns: e98bcc5c5be6f11dc582ae55f520d1ec4ae29f7a

While this time my Java impl returns: c19fccf57c613f1868dd22d586f9571cf6412cd0

The hash returned by my PHP code is not equal to the value returned by my Java function, and I can't find out why.

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

左秋 2024-11-22 02:01:15

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

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

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

On your PHP side, use single-quotes around the key so that the $ character is not treated as a variable reference. i.e.,

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

Otherwise, the key you really get is PRIE7-Yf17kEnUEpi5hvW/#AFo (assuming the variable $oG2uS is not defined).

清引 2024-11-22 02:01:15

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

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

The other answers pointing out HmacUtils from Apache commons have been deprecated by now. Apache commons now recommends using:

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

原野 2024-11-22 02:01:15

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

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

所示注意 $ 现在是 \$

Any $ symbol in double quotes ("") is regarded as a a variable in PHP. You can avoid the error by using either single quotes as pointed out by the previous commenter or you can escape the dollar sign as below

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

Notice $ is now \$

太傻旳人生 2024-11-22 02:01:15

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

In Java, and using maven:

Add the below dependency into the pom.xml:

 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.4</version>
    </dependency>

and then try to sign it using this

HmacUtils.hmacSha1Hex(key, string_to_sign);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文