Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs md5 with base64 digest algorithm wrong result

here is my code

var sig = crypto.createHash('md5')
  .update('The quick brown fox jumps over the lazy dog')
  .digest('base64');
console.log(sig)

results in nhB9nTcrtoJr2B01QqQZ1g== (on Mac OS X).

I'm trying to generate the same signature from an ios app. The results are the same in objective c as in online converter sites: the string

The quick brown fox jumps over the lazy dog

converted to md5, I get 9e107d9d372bb6826bd81d3542a419d6,

and the base64 of this is OWUxMDdkOWQzNzJiYjY4MjZiZDgxZDM1NDJhNDE5ZDY=.

Why are those strings different? Isn't this what nodejs crypto module is doing? What are the equivalent of nodejs algorithm for getting the md5 hash digested with base64?

like image 649
nikravi Avatar asked Jan 17 '13 21:01

nikravi


1 Answers

The string OWUxMDdkOWQzNzJiYjY4MjZiZDgxZDM1NDJhNDE5ZDY= is the base64 encoded version of the string 9e107d9d372bb6826bd81d3542a419d6 which is in it self the md5 hash of the plain text string The quick brown fox jumps over the lazy dog.

If you want to do this in node you first have to get the md5 hash in hex:

var crypto = require('crypto');
var s = 'The quick brown fox jumps over the lazy dog';
var md5 = crypto.createHash('md5').update(s).digest('hex');

Now you have the md5 hash as hex (9e107d9d372bb6826bd81d3542a419d6). Now all you have to do is convert it to base64:

new Buffer(md5).toString('base64');
like image 59
Thomas Watson Avatar answered Oct 13 '22 00:10

Thomas Watson