简体   繁体   English

为什么Java1 sha1和PHP5 sha1为utf-8字符串生成不同的结果?

[英]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? 我有一个包含一些utf-8字符的字符串,例如“ abc艾”,发现php5 sha1与Java脚本sha1生成了不同的代码,有人可以帮我吗? Thanks in advance. 提前致谢。

phpcode: phpcode:

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

result is 5345129746e444693aa1111c5840e4b57236f002 结果是5345129746e444693aa1111c5840e4b57236f002

javascript code: JavaScript代码:

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

result is 8a2aa0fed185dcffb922b1c4d67a49105525bd6a 结果是8a2aa0fed185dcffb922b1c4d67a49105525bd6a

The result you are getting from PHP is correct for the string encoded as GB18030 ( 61 62 63 B0 AC ) 从PHP获得的结果对于编码为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 ). 您从CryptoJS那里获得的代码对于编码为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. 您的PHP源文件使用错误的字符串编码保存,因此得到的结果不具有代表性。

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". 简而言之,您要处理的“艾”是Unicode字符,而不是“ UTF-8字符”。 There are multiple ways in which it may be represented by different systems, and these different representations have different SHA1 sums. 它可以通过多种方式由不同的系统表示,并且这些不同的表示具有不同的SHA1和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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