Search code examples
phphashnode.jsmd5signature

How to make hash in PHP and Node.js have the same value?


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.


Solution

  • 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.