Reputation: 4229
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.
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
Upvotes: 1
Views: 988
Reputation: 944474
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.
Upvotes: 5