The solution for node. JS md5 encryption is inconsistent between Chinese and php results,
Because of project requirements, Node. js and PHP need to call interfaces. It is found that node. js uses md5 encryption for Chinese characters and php does not have the same md5 encryption results for Chinese characters.
PHP
<? Php $ str = 'programmer '; echo $ str. 'md5:'. md5 ($ str);?>
Output:
Programmer md5: 72d9adf4944f23e5efde37f6364c126f
Node. JS
Var crypto = require ('crypto'); var md5 = function (str) {var crypto_md5 = crypto. createHash ('md5'); crypto_md5.update (str); return crypto_md5.digest ('hex');} var str = 'programmer '; var result = str + 'md5: '+ md5 (str); console. log (result );
Output:
Programmer md5: 1b6ad7846ec870e78363d1db4fb2a0ff
After checking the information, we found that the encoding problem was solved by adding the encoding in node. JS.
The solution is as follows:
Var crypto = require ('crypto'); var md5 = function (str) {var crypto_md5 = crypto. createHash ('md5'); crypto_md5.update (str, 'utf8'); // encode return crypto_md5.digest ('hex');} var str = 'programmer '; var result = str + 'md5: '+ md5 (str); console. log (result );
Output:
Programmer md5: 72d9adf4944f23e5efde37f6364c126f
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!