HMAC-SHA256 签名计算算法 [英] HMAC-SHA256 Algorithm for signature calculation

查看:79
本文介绍了HMAC-SHA256 签名计算算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 HMAC-SHA256 算法创建签名,这是我的代码.我使用的是美国 ASCII 编码.

I am trying to create a signature using the HMAC-SHA256 algorithm and this is my code. I am using US ASCII encoding.

final Charset asciiCs = Charset.forName("US-ASCII");
final Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(asciiCs.encode("key").array(), "HmacSHA256");
sha256_HMAC.init(secret_key);
final byte[] mac_data = sha256_HMAC.doFinal(asciiCs.encode("The quick brown fox jumps over the lazy dog").array());
String result = "";
for (final byte element : mac_data)
{
    result += Integer.toString((element & 0xff) + 0x100, 16).substring(1);
}
System.out.println("Result:[" + result + "]");

我从上面的代码得到的结果是:

The result that I am getting from the above code is:

f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8

这与wiki中显示的相同

This is same as to that of shown in the wiki

HMAC_SHA256("key", "The quick brown fox jumps over the lazy dog") = 0x f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8

除了对于0x.

我正在寻找想法/评论,如果我做的一切正确,或者我可以改进我的代码.

I am looking for ideas/comments if I am doing everything right or may be I can improve my code.

推荐答案

0x 只是表示它后面的字符代表一个十六进制字符串.

The 0x just denotes that the characters after it represent a hex string.

0x1A == 1Ah == 26 == 1A

所以0x只是为了说明输出的格式,不用担心.

So the 0x is just to clarify what format the output is in, no need to worry about it.

这篇关于HMAC-SHA256 签名计算算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆