简体   繁体   中英

Why Javascript sha1 and PHP5 sha1 generate different result for utf-8 string?

I have a string which contains some utf-8 characters, like "abc艾", and found that php5 sha1 generate a different code compared with Javascript sha1, could anyone help me this out? Thanks in advance.

phpcode:

$str = "abc艾";
$result = sha1($str);

result is 5345129746e444693aa1111c5840e4b57236f002

javascript code:

var str = "abc艾"
var result = sha1(str)

result is 8a2aa0fed185dcffb922b1c4d67a49105525bd6a

The result you are getting from PHP is correct for the string encoded as GB18030 ( 61 62 63 B0 AC )

The one you're getting from CryptoJS is correct for the string encoded as UTF-8 ( 61 62 63 E8 89 BE ).

There is no conflict here. Your PHP source file is saved using the incorrect string encoding, so the result you're getting is not representative.

Please read What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text . In short, what you are dealing with as "艾" is a Unicode character, not a "UTF-8 character". There are multiple ways in which it may be represented by different systems, and these different representations have different SHA1 sums.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM