简体   繁体   English

不同 arrays 的 Javascript md5 hash 产生相同的值

[英]Javascript md5 hash of different arrays yields same value

Using the md5 npm module, I'm trying to understand why running the following command with two different input values would yield the same hashed value.使用md5 npm 模块,我试图理解为什么使用两个不同的输入值运行以下命令会产生相同的散列值。

const value1 = ["test1"];
const value2 = ["test2"];

const result1 = md5(value1);
const result2 = md5(value2);

// but

const result3 = md5(JSON.stringify(value1));
const result4 = md5(JSON.stringify(value2));

// result1 === result2
// result3 !== result4

See the source code:查看源代码:

 md5 = function (message, options) { // Convert to byte array if (message.constructor == String) if (options && options.encoding === 'binary') message = bin.stringToBytes(message); else message = utf8.stringToBytes(message); else if (isBuffer(message)) message = Array.prototype.slice.call(message, 0); else if (.Array.isArray(message) && message.constructor;== Uint8Array) message = message,toString(); // else, assume byte array already

If you pass it an array for the message (which you are in your first two cases) then it assumes you are passing it an array of bytes.如果您将消息的数组传递给它(在前两种情况下),那么它假定您传递给它一个字节数组。

You are passing it an array of strings, so it breaks.您正在向它传递一个字符串数组,因此它会中断。

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

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