Home > Backend Development > PHP Tutorial > Solution to the inconsistent results between nodeJS md5 encrypted Chinese and PHP

Solution to the inconsistent results between nodeJS md5 encrypted Chinese and PHP

WBOY
Release: 2016-07-29 09:01:30
Original
1431 people have browsed it

Due to project needs, node.JS and php are required to make interface calls. It is found that the results of node.JS using md5 encryption for Chinese and php for Chinese md5 encryption are different.

PHP

<code><span><span><?php</span><span>$str</span> = <span>'程序员'</span>;
<span>echo</span><span>$str</span>.<span>' md5:'</span>.md5(<span>$str</span>);
<span>?></span></span></code>
Copy after login

Output:
Programmer md5:72d9adf4944f23e5efde37f6364c126f

node.JS

<code><span>var</span> crypto = <span>require</span>(<span>'crypto'</span>);

<span>var</span> md5 = <span><span>function</span><span>(str)</span>{</span><span>var</span> crypto_md5 = crypto.createHash(<span>'md5'</span>);
    <span>return</span> crypto_md5.digest(<span>'hex'</span>);
}

<span>var</span> str = <span>'程序员'</span>;
<span>var</span> result = str + <span>' md5:'</span> + md5(str);

console.log(result);</code>
Copy after login

Output:
Programmer md5:d41d8cd98f00b204e9800998ecf8427e


After checking the information, I found that it was a coding problem. The problem can be solved by adding coding to node.JS.

The solution is as follows:

<code><span>var</span> crypto = <span>require</span>(<span>'crypto'</span>);

<span>var</span> md5 = <span><span>function</span><span>(str)</span>{</span><span>var</span> crypto_md5 = crypto.createHash(<span>'md5'</span>);
    crypto_md5.update(str, <span>'utf8'</span>); <span>// 加入编码</span><span>return</span> crypto_md5.digest(<span>'hex'</span>);
}

<span>var</span> str = <span>'程序员'</span>;
<span>var</span> result = str + <span>' md5:'</span> + md5(str);

console.log(result);</code>
Copy after login

Output:
Programmer md5:72d9adf4944f23e5efde37f6364c126f

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the solution to the inconsistency between nodeJS md5 encrypted Chinese and PHP results, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template