【问题标题】:Difference of sha1 hashes in PHP and Node.JS for cyrillic西里尔文 PHP 和 Node.JS 中 sha1 哈希值的差异
【发布时间】:2013-04-18 13:04:58
【问题描述】:

如果我尝试从“ABC”获取 sha1,它们在 PHP 和 Node.JS 中是相同的。

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

但是,如果我尝试使用像这样的西里尔字母的散列:“ЭЮЯЁ”,它们不是。

如何解决?

【问题讨论】:

  • 您是否对两者使用了相同的编码?

标签: php node.js sha


【解决方案1】:

这个问题很可能是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 时,您会更频繁地遇到冲突。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2023-03-22
    • 2012-12-27
    相关资源
    最近更新 更多