如果我尝试从“ABC”获取 sha1,如果是 PHP 和 Node.JS,它们是相同的。
function sha1(input) {
return crypto.createHash('sha1').update(input).digest('hex');
};
但是如果我尝试对像这样的西里尔字母进行哈希处理:“ЭЮЯЁ”,它们就不是。
如何解决?
最佳答案
问题很可能是 character set/encodings aren't matching .
如果 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'
将有更好的匹配机会,因为这些都是 ASCII字符和 ASCII 是许多其他字符集的起点。当您超出 ASCII 范围时,您会更经常遇到冲突。
关于php - PHP 和 Node.JS 中西里尔文 sha1 哈希值的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16201155/