Home >Backend Development >PHP Tutorial >Solution to the inconsistent results between nodeJS md5 encrypted Chinese and PHP
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>
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>
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>
Output:
Programmer md5:72d9adf4944f23e5efde37f6364c126f
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.