The solution for node. JS md5 encryption is inconsistent between Chinese and php results,

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.