crypto,节点6返回与node4不同的散列

时间:2017-01-24 09:54:59

标签: node.js cryptography

升级nodeJS从4.2.6到6.9.1,加密HMAC产生不同的输出。 例如: 节点4.2.6:

crypto.createHmac('sha512', crypto_key_bytes).update(crypto_text).digest('hex')
.toUpperCase()

=> 20404FCB6D86CDF0E38002DD8BC36596C2882EB48433C074F9DDC2F1F6D47748E1F26E062E2D17671C18B87FFEE1C72576B48CFA9A61AF447A2F4C1B06316616 可以

节点6.9.1:

crypto.createHmac('sha512',crypto_key_bytes).update(crypto_text)
.digest('hex').toUpperCase();

crypto.createHmac('sha512',crypto_key_bytes2).update(crypto_text,'binary')
.digest('hex').toUpperCase();

crypto.createHmac('sha512',crypto_key_bytes2)
.update(crypto_text,'utf-8').digest('hex').toUpperCase();<code

=&gt; 1D974668D0CB06B87C9645CF92161358951B224798015BAEE5A4BEDC54E88159E5082C6E3BB1D8612C904C33F9A80A88642ECB99B69B7BBDC5EC633119169DBE 不行

你知道吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

我相信你遇到的问题是数据,而不是Node.js HMAC实现。

我用4.2.6和6.9.1进行了快速测试,他们确实生产了相同的'sha512'HMAC。

<强>代码

var  crypto = require('crypto');
console.log(
    crypto.createHmac('sha512',"key").update("text").digest('hex').toUpperCase()
);

<强>输出

./node --version
4.2.6 

./nodejs hmac
B585312ACDD38EC13F13BB4CBA35A75473F32B6AE4A0303926815BD43D7A2631516B2B031F34D89EDA853E948D5057DE54A880C16697242DBE6A1AD994BC4E5D

./node --version
v6.9.1

./nodejs hmac
B585312ACDD38EC13F13BB4CBA35A75473F32B6AE4A0303926815BD43D7A2631516B2B031F34D89EDA853E948D5057DE54A880C16697242DBE6A1AD994BC4E5D

答案 1 :(得分:1)

问题导致功能:

  

for(var k=0 ; k < crypto_key.length-1 ; k+=2){ tmp = parseInt(crypto_key.substr(k, 2), 16); crypto_key_bytes.push(tmp); }

更正:

for(var k=0 ; k < crypto_key.length-1 ; k+=2){
tmp = parseInt(crypto_key.substr(k, 2), 16);
if (tmp>128) {
tmp = tmp-256;
}
crypto_key_bytes.push(tmp);
}`

fonction parseInt()从NodeJS 4.2.6更改为6.9.1 ..

相关问题