Why are Javascript sha1 and PHP5 sha1 generating a different result for utf-8 string?

I have a line that contains some utf-8 characters, such as "abc 艾", and found that php5 sha1 is generating different code compared to Javascript sha1, can anyone help me with this? Thanks in advance.

phpcode:

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

result 5345129746e444693aa1111c5840e4b57236f002

Javascript code:

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

result 8a2aa0fed185dcffb922b1c4d67a49105525bd6a

+4
source share
1 answer

The result you get from PHP is correct for a string encoded as GB18030 ( 61 62 63 B0 AC )

The one you get from CryptoJS is correct for a string encoded as UTF-8 ( 61 62 63 E8 89 BE ).

There is no conflict. The PHP source file is saved using the wrong string encoding, so the result is not representative.

Please read What every programmer absolutely needs to know positively about encodings and character sets for working with text . In short, what you mean by “艾” is a Unicode character, not a “UTF-8 character”. There are several ways in which it can be represented by different systems, and these different representations have different amounts of SHA1.

+5
source

Source: https://habr.com/ru/post/1482839/


All Articles