简体   繁体   English

Java和JavaScript中不同的md5哈希结果

[英]Different md5 hash results in Java and JavaScript

I'm trying to translate existing code from java into JavaScript (node.js to be specific). 我正在尝试将现有代码从Java转换为JavaScript(具体来说是node.js)。 Creating an md5 hash of the same String in both languages leads to the following different results: 使用两种语言创建相同String的md5哈希值会导致以下不同结果:

In Java Arrays.toString(MessageDigest.getInstance("MD5").digest("test".getBytes())); Java中 Arrays.toString(MessageDigest.getInstance("MD5").digest("test".getBytes()));

returns 退货

[9, -113, 107, -51, 70, 33, -45, 115, -54, -34, 78, -125, 38, 39, -76, -10] [9,-113,107,-51,70,33,-45,115,-54,-34,78,-125,38,39,-76,-10]

while in JS crypto.createHash("md5").update("test", "ascii").digest(); 而在JS crypto.createHash("md5").update("test", "ascii").digest();

returns 退货

[9, 143, 107, 205, 70, 33, 211, 115, …] [9,143,107,205,70,33,211,115,…]

using crypto 1.0.1 Can anyone explain this to me? 使用crypto 1.0.1有人可以向我解释吗? I already played around with different encodings, but it did not affect the result. 我已经使用了不同的编码,但是并没有影响结果。

You're using signed bytes in Java. 您正在Java中使用带符号的字节。 It's actually the same. 实际上是一样的。 I would vote for the unsigned version, but if you have to be compatible with the Java version just arr.map(function(e) {return e >= 128 ? e - 256 : e}) 我会投票支持未签名的版本,但是如果您必须与Java版本兼容,只需arr.map(function(e) {return e >= 128 ? e - 256 : e})

Both hashes are same. 这两个哈希是相同的。 Since the unsigned/signed types you are seeing different numbers but they has same values. 由于无符号/有符号类型,您会看到不同的数字,但是它们具有相同的值。 Try casting values to unsigned. 尝试将值强制转换为无符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM