11

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?

2
  • Just so I'm sure I understand the question. Do you want the md5 hash of the string 'The quick brown fox jumps over the lazy dog' encoded into base64 or do you want the string it self converted into base64? Commented Jan 17, 2013 at 22:03
  • @ThomasWatson, I want the base64 encoded string of the md5 hash of the The quick brown fox.. Commented Jan 17, 2013 at 22:11

1 Answer 1

17

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Can't you just use .digest().toString('base64')? BTW you'd need new Buffer(md5, 'hex') for this to work properly, now you're getting base64 of hex digest, not of the original binary hash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.