HMAC-SHA256签名计算算法
我正在尝试使用 HMAC-SHA256 算法创建签名,这是我的代码。 我使用的是 US ASCII 编码。
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 + "]");
我从上面的代码得到的结果是:
f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
这与 wiki 中显示的结果相同,
HMAC_SHA256("key", "The quick brown fox jumps over the lazy dog") = 0x f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
除了 0x
。
如果我做的一切正确或者我可以改进我的代码,我正在寻找想法/评论。
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
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
except for the 0x
.
I am looking for ideas/comments if I am doing everything right or may be I can improve my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是我的解决方案:
或者您可以返回以 Base64 编码的哈希值:
十六进制输出符合预期:
Here is my solution:
Or you can return the hash encoded in Base64:
The output in hex is as expected:
0x 只是表示它后面的字符代表一个十六进制字符串。
所以0x只是为了明确输出的格式,无需担心。
The 0x just denotes that the characters after it represent a hex string.
So the 0x is just to clarify what format the output is in, no need to worry about it.
如果您使用的是 Guava,其最新版本现在允许您使用
使用此功能的一个示例:
此处有更多文档:https://guava.dev/releases/23.0/api/docs/com/google/common/hash/Hashing.html#hmacSha256-byte:A-
If you're using Guava, its latest release now lets you use
One example of using this:
Further documentation here: https://guava.dev/releases/23.0/api/docs/com/google/common/hash/Hashing.html#hmacSha256-byte:A-
您在那里得到的答案是正确的。上面代码中的一件小事是,您需要先 init(key) ,然后才能调用 doFinal()
The answer that you got there is correct. One minor thing in the code above, you need to init(key) before you can call doFinal()
这对我来说工作正常,
我添加了依赖
项参考: http://mvnrepository。 com/artifact/commons-codec/commons-codec/1.9
我的函数
This is working fine for me
I have add dependency
ref: http://mvnrepository.com/artifact/commons-codec/commons-codec/1.9
my function
试试这个
抱歉迟到了,我已经尝试了上述所有答案,但没有一个给我正确的价值,在做了很多研发之后发现了一个简单的方法,可以给我准确的价值。
在您的类中声明此方法
}
像这样使用
像Verification
1.Android studio 输出
2. 在线 HMAC 生成器输出(在线生成器请访问此处)
Try this
Sorry for being late, I have tried all above answers but none of them is giving me correct value, After doing the lot of R&D I have found a simple way that gives me exact value.
Declare this method in your class
}
Use this like
Verification
1.Android studio output
2. Online HMAC generator Output(Visit here for Online Genrator)
用于生成编码 (HMAC-x) 签名的 Java 简单代码。 (尝试使用 Java-8 和 Eclipse)
Java simple code to generate encoded(HMAC-x) signatures. (Tried using Java-8 and Eclipse)
如果您有机会在这里找到如何计算 HMAC-SHA256 的解决方案,但您会遇到如下异常:
然后使用:
If but any chance you found a solution how to calculate HMAC-SHA256 here, but you're getting an exception like this one:
Then use:
这是我的解决方案:
Here is my solution: