3

In node.js, I have:

var h = crypto.createHash("md5");   // md5
h.update("AAA");
h.digest("hex");

In PHP I have:

md5("AAA");

However, both have different value. How can I make it the same? or else, what other algorithm I should use to make them the same, so that I can use it as signature calculation. Thanks.


Oppss.. actually. my mistake. when I test it, there is a bug.. it will md5 the same thing.

3
  • Those both output the exact same value for me: e1faffb3e614e6c2fba74296962386b7. What versions of node and php are you using? Commented Jul 19, 2011 at 23:06
  • You should create hash in one statement as below... Commented Jul 19, 2011 at 23:20
  • If your string contents non 1-byte length characters (l'Entrecôte for example) use Buffer instead string --> crypto.createHash('md5').update(new Buffer(ts)).digest('hex') Commented Dec 1, 2015 at 5:19

4 Answers 4

10

Simple googling I did in the past gave me => http://japhr.blogspot.com/2010/06/md5-in-nodejs-and-fabjs.html

Node.js

Script:

var crypto = require('crypto');
var hash = crypto.createHash('md5').update('AAA').digest("hex");
console.log(hash);

Output:

alfred@alfred-laptop:~/node/hash$ node hash.js 
e1faffb3e614e6c2fba74296962386b7

PHP

Code

<?php
echo md5("AAA");

Output:

alfred@alfred-laptop:~/node/hash$ php md5.php 
e1faffb3e614e6c2fba74296962386b7

The output for both PHP and node.js are equal.

C extension

Also you might have look at https://github.com/brainfucker/hashlib which uses C implementation which is going to be faster.

Sign up to request clarification or add additional context in comments.

1 Comment

Test your code with string that contains non 1-byte characters ant it will fail. For example l'Entrecôte
1

Your code creates the same hash value for me, you might doing it wrong at some point

Comments

0

I had the same issue with creating hash for non UTF8 string :

var non_utf8_str = "test_merchant;www.market.ua;DH783023;1415379863;1547.36;UAH;Процессор
Intel Core i5-4670 3.4GHz;Память Kingston DDR3-1600 4096MB
PC3-12800;1;1;1000;547.36";

The results in PHP and NodeJS was different until I used utf8 library. So following code works equivalent for both PHP and NodeJS :

crypto.createHash('md5').update(utf8.encode(non_utf8_str)).digest('hex');

Comments

0
var hash = crypto.createHash('md5').update(password, 'latin1', 'latin1').digest('hex');

This worked for me. Try different encodings: 'utf8', 'ascii', or 'latin1'.

Comments

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.