简体   繁体   English

西里尔语的PHP和Node.JS中sha1哈希的区别

[英]Difference of sha1 hashes in PHP and Node.JS for cyrillic

If i try to get sha1 from "ABC" they are same if PHP and Node.JS. 如果我尝试从“ ABC”中获取sha1,则它们与PHP和Node.JS相同。

function sha1(input) {
  return crypto.createHash('sha1').update(input).digest('hex');
};

But if i try to take hash of something cyrillic like this: "ЭЮЯЁ" they are not. 但是,如果我尝试散列像这样的西里尔字母:“ЭЮЯЁ”,它们就不是。

How to fix it? 如何解决?

The issue is likely that the character set/encodings aren't matching . 问题可能是字符集/编码不匹配

If the string in PHP is UTF-8 encoded, you can mirror that in Node.js by specifying 'utf8' : 如果PHP中的字符串是UTF-8编码的,则可以通过指定'utf8'在Node.js中进行镜像:

function sha1(input) {
  return crypto.createHash('sha1').update(input, 'utf8').digest('hex');
};
> crypto.createHash('sha1').update('ЭЮЯЁ').digest('hex')
'da7f63ac9a3b5c67c8920871145cb5904f3df29a'
> crypto.createHash('sha1').update('ЭЮЯЁ', 'utf8').digest('hex')
'f78c3521413a8321231e35665f8c4a16550e182a'

'ABC' will have a better chance of matching because these are all ASCII characters and ASCII is a starting point for many other character sets. 'ABC'匹配的机会更大,因为它们都是ASCII字符,而ASCII是许多其他字符集的起点。 It's when you get beyond ASCII that you'll more often run into conflicts. 当您超出ASCII时,您将更经常遇到冲突。

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

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