[英]How can I replicate a javascript sha256 hash in Java and get the same Hex output?
I need to replicate a javascript sha256 hash in java in my groovy/java application.我需要在我的 groovy/java 应用程序中的 java 中复制一个 javascript sha256 哈希。
The javascript version uses the hash function included in angular and I have no control over it. javascript 版本使用 angular 中包含的哈希函数,我无法控制它。 Given the same input strings, I need to provide the same Hex output.
给定相同的输入字符串,我需要提供相同的十六进制输出。
In java, i'm using https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html在java中,我使用https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html
In java:在Java中:
DigestUtils.sha256(cx2 + username):
gives me a bytestring something that gets printed as:给我一个字节串,它被打印为:
[-114, -15, 57, -56, 81, 37, -95, 119, 102, 81, 63, 99, -3, -56, -116, -110, -114, -16, -18, 117, 118, 49, -120, 14, 68, 30, -37, 20, -70, -17, -19, -88] [-114, -15, 57, -56, 81, 37, -95, 119, 102, 81, 63, 99, -3, -56, -116, -110, -114, -16, -18, 117, 118, 49, -120, 14, 68, 30, -37, 20, -70, -17, -19, -88]
In java script:在java脚本中:
var s1 = Sha256.hash(cx2 + username)
gives me a bytestring (Javascript's type of will say it is a String though) something that gets printed as below(not sure what encoding is that):给了我一个字节串(尽管 Javascript 的类型会说它是一个字符串),它会打印如下(不确定是什么编码):
ñ9ÈQ%¡wfQ?cýÈðîuv1DÛºïí¨ ñ9ÈQ%¡wfQ?cýÈðîuv1DÛºïí¨
If I convert both bytestrings to Hex, I get the same result both in java and javascript :如果我将两个字节串都转换为十六进制,我会在 java 和 javascript 中得到相同的结果:
console.log Sha256.toHexStrfromByteStr(s1)
// 478972ab3380187060494987ac7c597ac92decdac1c04dd1dcab8184995ec01b
That would be it, except that the javascript code does a second hash concatenating the bytestring to another string:就是这样,除了 javascript 代码执行第二个哈希将字节串连接到另一个字符串:
var s2 = Sha256.hash(cx1 + s1)
When I try to replicate the second hash in Java, i get a very different output(after converting both outputs to hex).当我尝试在 Java 中复制第二个哈希时,我得到了一个非常不同的输出(在将两个输出都转换为十六进制之后)。
def s2 = DigestUtils.sha256(cx1 + s1)
Is there a step that i'm missing?有没有我遗漏的步骤?
import org.apache.commons.codec.digest.DigestUtils
String cx2 = 'Potato'
String cx1 = 'Bread'
def s1 = DigestUtils.sha256(cx2 + 'username')
def s2 = DigestUtils.sha256Hex(cx1 + s1)
println s2
Javascript Javascript
var s1 = Sha256.hash(cx2 + 'username');
var s2 = Sha256.hash(cx1 + s1);
console.log (Sha256.toHexStrfromByteStr(s2))
Thanks a million!太感谢了!
By doing通过做
def s1 = DigestUtils.sha256(cx2 + 'username')
result s1
has type byte[]
.结果
s1
类型为byte[]
。 Then, by doing然后,通过做
def s2 = DigestUtils.sha256Hex(cx1 + s1)
you're concatenating result of <byte []>.toString()
to cx1
.您将
<byte []>.toString()
结果连接到cx1
。
You must instead:你必须:
a) convert s1
to "normal" String - new String(s1)
- and concat two strings, or a) 将
s1
转换为“普通”字符串 - new String(s1)
- 并连接两个字符串,或
b) convert cx1
to byte[]
and concat two arrays. b) 将
cx1
转换为byte[]
并连接两个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.