-1

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.

  • @RolandStarke it's not the same, it was the first thing I tried, obviously – Alexander Nuñez May 24 '18 at 16:21
  • `9860bd2248c069c7b65045917c215596` is not correct, it should be: `b8ee918f782fe7135b25c1fa59339094`, look at my answer, which is correct. – Marcos Casagrande May 24 '18 at 16:50
  • @MarcosCasagrande please,just run the second snippet and see it yourself – Alexander Nuñez May 24 '18 at 16:54
  • I see it, and your snippet is wrong as I stated. your expected digest is **wrong**, the correct `md5` for that string, is `b8ee918f782fe7135b25c1fa59339094`, my answer is correct. – Marcos Casagrande May 24 '18 at 16:55
  • I don't know if you're doing it on purpose or not, what is it to hard to understand that: `9860bd2248c069c7b65045917c215596` is **not correct**, "other sites such as" **does not support your claim** you're just using the same code, that it isn't **correct**. – Marcos Casagrande May 24 '18 at 17:21
  • @MarcosCasagrande as I stated above, it is not my code,and the only thing I want is to be able to replicate such output using PHP, I'm not interested in the correctness of such snippet of code.Let me reformulate the question:Is there a PHP code equivalent to the first node js snippet, which outputs the expect string "9860bd2248c069c7b65045917c215596"? – Alexander Nuñez May 24 '18 at 19:01
  • I Updated my answer with the php equivalent. – Marcos Casagrande May 24 '18 at 20:21
  • @MarcosCasagrande thanks for the proposed solution, it is solved now, the issue here was due to the ¤ symbol. – Alexander Nuñez May 26 '18 at 16:48
  • Yeah, because you're converting it to 'binary'. But you should know for other projects, that you should just send the string as is, to `.update` if you want the correct & universal md5 :) – Marcos Casagrande May 26 '18 at 16:50

1 Answers1

1

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.

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98