Search code examples
phpnode.jsmd5cryptojsdigest

what would the equivalent php version be of the following nodejs md5 hashing source code?


I'm migrating from nodejs to PHP and I couldn't obtain a similar output md5 hash digest for the below snippet having the same input.Perhaps there's something I'm missing.

var md5sum = crypto.createHash('md5');
md5sum.update(new Buffer(str, 'binary'));
md5_result = md5sum.digest('hex');

Thanks in advance for your help!!!, Btw, my nodejs version is 10.1.0, and npm version is 5.6.0. And for the ones asking, this source code equivalent is not md5($str) and it is not my code, I'm just converting it. For example, for the following input 42b86318d761e13ef90c126c3e060582¤3¤724039¤1 the obtained digest is 9860bd2248c069c7b65045917c215596.

I just tried to run the following snippet at https://www.tutorialspoint.com/execute_nodejs_online.php, taking into account your proposals but they don't work:

const crypto = require('crypto');
var str = "42b86318d761e13ef90c126c3e060582¤3¤724039¤1";
var md5sum = crypto.createHash('md5');
md5sum.update(new Buffer(str, 'binary'));
const md5_result = md5sum.digest('hex');
const md5 = crypto.createHash('md5').update(str).digest('hex');
const expected_digest = "9860bd2248c069c7b65045917c215596";
console.log("original version digest:" + md5_result);
console.log("proposed equivalent digest:" + md5);
console.log("expected digest:" + expected_digest);

What I get on that site is: original version digest:9860bd2248c069c7b65045917c215596 proposed equivalent digest:b8ee918f782fe7135b25c1fa59339094 expected digest:9860bd2248c069c7b65045917c215596

Other sites such as https://www.katacoda.com/courses/nodejs/playground,https://repl.it/ ,https://www.jdoodle.com/execute-nodejs-online support my claim (i.e. md5 digest is 9860bd2248c069c7b65045917c215596), however,so far, this site http://rextester.com/l/nodejs_online_compiler outputs what some of you obtained(i.e. b8ee918f782fe7135b25c1fa59339094). As I said before, please, help me find a PHP EQUIVALENT version of the first nodejs snippet of code.


Solution

  • You shouldn't use: new Buffer(str, 'binary') just:

    const md5 = crypto
        .createHash('md5')
        .update(string)
        .digest('hex');
    

    Using that, you will get the same output with php md5, linux md5sum, and node.

    For your input: 42b86318d761e13ef90c126c3e060582¤3¤724039¤1 the following commands will print the same:

    md5sum

    echo -n "42b86318d761e13ef90c126c3e060582¤3¤724039¤1" | md5sum
    

    PHP

    echo md5("42b86318d761e13ef90c126c3e060582¤3¤724039¤1");
    

    Node

    require('crypto')
            .createHash('md5')
            .update("42b86318d761e13ef90c126c3e060582¤3¤724039¤1")
            .digest('hex');
    

    All three will output: b8ee918f782fe7135b25c1fa59339094

    NOTE:

    new Buffer is deprecated, Buffer.from should be used instead.

    Other sites such as https://www.katacoda.com/courses/nodejs/playground,https://repl.it/ ,https://www.jdoodle.com/execute-nodejs-online support my claim (i.e. md5 digest is 9860bd2248c069c7b65045917c215596)

    They're not supporting your claim, you're executing the same code, which is wrong, on many different node.js environment. Of course every Node.js environment will print that output for your code, that doesn't make it right.


    Since you can't modify the code, and you want the PHP equivalent, here it is:

    function utf8_char_code_at($str, $index) {
        $char = mb_substr($str, $index, 1, 'UTF-8');
    
        if (mb_check_encoding($char, 'UTF-8')) {
            $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
            return hexdec(bin2hex($ret));
        } else {
            return null;
        }
    }
    
    function myMD5($str) {
    
         $tmp = "";
    
         for($i = 0; $i < mb_strlen($str); $i++)
            $tmp .= bin2hex(chr(utf8_char_code_at($str, $i)));
    
         return md5(hex2bin($tmp));
    }
    
    echo myMD5($string);
    

    utf8_char_code_at taken from: https://stackoverflow.com/a/18499265/1119863

    It will output: 9860bd2248c069c7b65045917c215596 same as your node snippet.