以下nodejs md5哈希源代码的等效php版本是什么? [英] what would the equivalent php version be of the following nodejs md5 hashing source code?

查看:75
本文介绍了以下nodejs md5哈希源代码的等效php版本是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从nodejs迁移到PHP,但对于以下具有相同输入的代码片段,我无法获得类似的输出md5哈希摘要.也许是我缺少了一些东西.

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');

在此先感谢您的帮助!,顺便说一句,我的nodejs版本是10.1.0,npm版本是5.6.0.对于那些问的人来说,等效的源代码不是 md5($ str),也不是我的代码,我只是在转换它.例如,对于以下输入42b86318d761e13ef90c126c3e060582¤3¤724039¤1,获得的摘要为 9860bd2248c069c7b65045917c215596 .

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.

我只是尝试在 https://www.tutorialspoint.com/execute_nodejs_online上运行以下代码段.php ,考虑到您的建议,但它们不起作用:

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);

我在该网站上得到的是:原始版本摘要:9860bd2248c069c7b65045917c215596建议的等效摘要:b8ee918f782fe7135b25c1fa59339094预期摘要:9860bd2248c069c7b65045917c215596

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

其他网站,例如 https://www.katacoda.com/courses/nodejs/playground https://repl.it/ http://rextester.com/l/nodejs_online_compiler 输出您从中获得的一些信息(即 b8ee918f782fe7135b25c1fa59339094 ).正如我之前所说,请帮助我找到第一个nodejs代码片段的PHP等效版本.

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.

推荐答案

您不应使用: new Buffer(str,'binary')只是:

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

使用php md5 ,linux md5sum 和node,您将获得相同的输出.

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

对于您的输入:42b86318d761e13ef90c126c3e060582¤3¤724039¤1以下命令将显示相同的内容:

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");

节点

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

这三个都将输出: b8ee918f782fe7135b25c1fa59339094

注意:

new缓冲区,而应使用 Buffer.from .

其他网站,例如 https://www.katacoda.com/courses/nodejs/playground,https://repl.it/ https://www.jdoodle.com/execute-nodejs-online 支持我要求(即md5摘要为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)

他们不支持您的主张,您正在许多不同的node.js环境中执行相同的代码,这是错误的.当然,每个Node.js环境都会为您的代码打印该输出,但这并不正确.

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.

由于您无法修改代码,并且想要使用等效的PHP,因此它是:

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 来自: https://stackoverflow.com/a/18499265/1119863

它将输出: 9860bd2248c069c7b65045917c215596 与您的节点代码段相同.

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

这篇关于以下nodejs md5哈希源代码的等效php版本是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆