I’m trying to generate a MD5 hash code in NodeJS and compare it with a php-generated MD5 hash code with number type ex: md5(10)
.
NodeJS does not seem to have a way to generate a MD5 hash with number type only – only String, Buffer, Array or Uint8Array types are supported.
I tried storing the number into a buffer, or a binary list but I did not manage to match the php-generated hash :
var buffer = new Buffer(16); buffer.writeDoubleBE(10) return crypto.createHash("md5").update(buffer).digest("hex"); // result : c6df1eeac30ad1fab5b50994e09033fb // PHP-generated hash : md5(10) = d3d9446802a44259755d38e6d163e820
What exactly MD5 in php do with the data type ?
Which data form should i use to get the same result ?
Advertisement
Answer
The md5 function in PHP calculates the hash of a string, so in your example it’s actually calculating the hash of “10”.
To do this in Node.js should be quite simple:
const dataToHash = Buffer.from(String(10), "utf-8"); const hash = crypto.createHash("md5").update(dataToHash).digest("hex"); console.log("Hash:", hash);
We get the same result in Node.js as in PHP, e.g.
Hash: d3d9446802a44259755d38e6d163e820