Wt Test
Wt Test

Reputation: 41

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

Upvotes: 4

Views: 1973

Answers (1)

user149341
user149341

Reputation:

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.

Upvotes: 7

Related Questions