简体   繁体   中英

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.

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' :

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. It's when you get beyond ASCII that you'll more often run into conflicts.

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