为什么crypto.createHash在新版本中返回不同的输出? [英] Why crypto.createHash returns different output in new version?
问题描述
问题
我的node.js模块使用 crypto.createHash
生成 md5
哈希。
I have node.js module that is using crypto.createHash
to generate md5
hash.
最近我注意到由 crypto生成的哈希
模块在新版本中有所不同:
Recently I noticed that hash generated by crypto
module is different in new versions:
代码
require('crypto').createHash('md5').update('¥').digest('hex')
Node.js v0.10.0
输出: ab3af8566ddd20d7efc9b314abe90755
<强>的Node.js V6.1.0 强>
输出: 07625e142e4ac5961de57472657a88c1
<强>问题强>
我想知道新版本的原因是什么,我该如何解决?
I was wondering what causes that in new version and how can I solve this?
更新
关于GitHub的类似问题:
Similar issues on GitHub:
- https://github.com/nodejs/node/issues/6813
- https://github.com/node-xmpp/client/issues/206
推荐答案
Node v6 +中的一些输入计算与以前的Node版本不同的哈希值。
Some inputs in Node v6+ calculate a different hash than previous Node versions.
基本上,当你传递一个字符串时 .update()
,在v6之前的Node版本中,默认编码为 binary
,但是对于更改为<的Node v6 code> utf-8 。
Basically, when you pass a string to .update()
, with Node versions before v6 the default encoding was binary
, but for Node v6 that changed to utf-8
.
例如,请使用以下代码:
For example, take this code:
require('crypto').createHash('md5').update('¥').digest('hex')
此输出 ab3af8566ddd20d7efc9b314abe90755
在节点预6和 07625e142e4ac5961de57472657a88c1 $节点6上的c $ c>。
如果您希望节点6输出与6前版本相同的内容,则必须告诉 .update ()
使用二进制
编码ing:
If you want Node 6 to output the same as pre-6 versions, you have to tell .update()
to use binary
encoding:
require('crypto').createHash('md5').update('¥', 'binary').digest('hex')
或者相反(使节点前6输出相同) as 6):
Or the other way around (make Node pre-6 output the same as 6):
require('crypto').createHash('md5').update('¥', 'utf-8').digest('hex')
这篇关于为什么crypto.createHash在新版本中返回不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!