-2

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 ?

giuseppedeponte
  • 2,366
  • 1
  • 9
  • 19
  • Looks like you have the same questions answered here: [what would the equivalent php version be of the following nodejs md5 hashing source code?](https://stackoverflow.com/questions/50513837/what-would-the-equivalent-php-version-be-of-the-following-nodejs-md5-hashing-sou) – Roman Dec 08 '20 at 08:16

1 Answers1

0

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
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40