带有重音符号的NodeJS hmac摘要问题 [英] NodeJS hmac digest issue with accents
问题描述
对于下面的代码,我正在与Ruby,PHP和NodeJS进行并排比较,使用crypto
模块在NodeJS中得到了错误的响应.
I'm doing a side by side comparison with Ruby, PHP and NodeJS for the following code, getting an incorrect response in NodeJS using the crypto
module.
PHP
hash_hmac('sha256', 'text', 'á');
Ruby
OpenSSL::HMAC.hexdigest('sha256', 'á', 'text')
NodeJS
var signer = crypto.createHmac('sha256', 'á');
var expected = signer.update("text").digest('hex');
Ruby和PHP都返回34b3ba4ea7e8ff214f2f36b31c6a6d88cfbf542e0ae3b98ba6c0203330c9f55b
,而NodeJS返回7dc85acba66d21e4394be4f8ead2a327c9f1adc64a99c710c98f60c425bd7411
.我注意到,如果我在PHP中尝试使用utf8_encode('á')
,它实际上会给我Node期望的结果.
Both Ruby and PHP return 34b3ba4ea7e8ff214f2f36b31c6a6d88cfbf542e0ae3b98ba6c0203330c9f55b
, while, NodeJS returns 7dc85acba66d21e4394be4f8ead2a327c9f1adc64a99c710c98f60c425bd7411
. I noticed that, if I try with utf8_encode('á')
in PHP, it actually gives me the result Node expects.
我正在从文件中将重音符号文本加载到文件中,如下所示:
I'm loading the accented text in Node from a file, like so:
JSON.parse(fs.readFileSync('keys.js', 'utf8'));
我该如何在Node中更改代码以获取PHP和Ruby都提供的结果哈希?
How would I go about changing my code in Node to get the resulting hash that both PHP and Ruby present?
谢谢!
推荐答案
此代码将为您提供正确的结果:
This code will give you the correct result:
var crypto = require('crypto');
var signer = crypto.createHmac('sha256', new Buffer('á', 'utf8'));
var result = signer.update("text").digest('hex');
console.log(result);
这篇关于带有重音符号的NodeJS hmac摘要问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!