Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 279
murvinlai Avatar asked Feb 06 '26 00:02

murvinlai


1 Answers

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.

like image 180
Alfred Avatar answered Feb 08 '26 12:02

Alfred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!