节点JS加密,无法在带有重音符号的字符上创建hmac [英] Node JS crypto, cannot create hmac on chars with accents

查看:156
本文介绍了节点JS加密,无法在带有重音符号的字符上创建hmac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试加密的文本带有重音字符(例如ä,ï,ë)时,我在使用Node.JS(使用crypto.js)中生成正确的签名时遇到问题

I am having an issue generating the correct signature in NodeJS (using crypto.js) when the text I am trying to encrypt has accented characters (such as ä,ï,ë)

generateSignature = function (str, secKey) { 
 var hmac = crypto.createHmac('sha1', secKey);
 var sig = hmac.update(str).digest('hex');
 return sig;
};

如果'str'不包含重音字符(如ä,ï,ë等字符),则此函数将返回正确的HMAC签名.如果文本中存在带重音符号的字符,它将不会返回正确的HMAC.带重音符号的字符在UTF8编码中有效,所以我不知道为什么加密对它们有问题.可能是这种情况,我需要以某种方式告诉加密货币,我正在对utf8编码的文本进行签名,但是我不知道该怎么做.

This function will return the correct HMAC signature if 'str' contains no accented characters (chars such as ä,ï,ë). If there are accented chars present in the text, it will not return the correct HMAC. The accented characters are valid in UTF8 encoding so I dont know why crypto has a problem with them. It may be the case that I need to somehow tell crypto that I am signing utf8 encoded text, but I don't know how to do this.

此帖子中描述了完全相同的问题:带有重音符号的NodeJS hmac摘要问题 但是,帖子本身以及答案对我来说都是没有意义的(因为他们正在传递要加密的数据,密钥应该放在哪里).

The exact same problem is described in this post: NodeJS hmac digest issue with accents However, the post itself, as well as the answer, do not make sense to me (as they are passing the data they want to encrypt where the secret key should go).

以下是该代码的版本,其中包含str和secKey的硬编码值:

Here is a version of the code with hard coded values for str and secKey:

  var crypto = require('crypto');

  str="äïë";  
  secKey="secret"; 
  var hmac = crypto.createHmac('sha1', secKey);
  var sig = hmac.update(new Buffer(str, 'utf8')).digest('hex');
  console.log("Sig:      " + sig);
  console.log("Expected: 094b2ba039775bbf970a58e4a0a61b248953d30b"); 
  // "Expected" was generated using http://hash.online-convert.com/sha1-generator

输出::

签名:39c9f1a6094c76534157739681456456e7878557f58

Sig: 39c9f1a6094c76534157739681456e7878557f58

预期:094b2ba039775bbf970a58e4a0a61b248953d30b

Expected: 094b2ba039775bbf970a58e4a0a61b248953d30b

谢谢

推荐答案

crypto模块使用的默认编码通常为 'binary' .因此,您必须通过Buffer指定'utf-8'才能将其用作编码:

The default encoding used by the crypto module is usually 'binary'. So, you'll have to specify 'utf-8' via a Buffer to use it as the encoding:

var sig = hmac.update(new Buffer(str, 'utf-8')).digest('hex');

这就是另一个问题的答案所展示的,只是针对关键问题:

That's what the answer for the other question was demonstrating, just for the key:

var hmac = crypto.createHmac('sha1', new Buffer(secKey, 'utf-8'));


您还可以使用 Buffer 查看差异:


You can also use Buffer to view the differences:

new Buffer('äïë', 'binary')
// <Buffer e4 ef eb>

new Buffer('äïë', 'utf-8')
// <Buffer c3 a4 c3 af c3 ab>


[编辑]

运行您提供的示例代码,我得到:

Running the example code you provided, I get:

Sig:      094b2ba039775bbf970a58e4a0a61b248953d30b
Expected: 094b2ba039775bbf970a58e4a0a61b248953d30b

然后,稍加修改,我得到true:

And, modifying it slightly, I get true:

var crypto = require('crypto');

function sig(str, key) {
  return crypto.createHmac('sha1', key)
    .update(new Buffer(str, 'utf-8'))
    .digest('hex');
}

console.log(sig('äïë', 'secret') === '094b2ba039775bbf970a58e4a0a61b248953d30b');

这篇关于节点JS加密,无法在带有重音符号的字符上创建hmac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆