hmac SHA256 base64和nodejs crypto的Cryptojs差异



让我们有简单的msg和键:

消息='simpl'

private_key ='123456789';在Angular Project与Cryptojs一起使用:

const signature = CryptoJS.HmacSHA256('simple', '123456789');
const signatureBase = signature.toString(CryptoJS.enc.Base64);

对我的结果是:

lvs7rqte1edtlas1gvwwsng5zayvch9aayc noeunc4 =

在sg中使用它,在节点中使用键:

var hmacsignature = crypto.createHmac('sha256', new Buffer("123456789", "base64"))
.update("simple")
.digest()
.toString('base64');

结果是:

nyu2pgqfrdwnhbt649q0gc 7dciq8iwcwcwcwcquqa5t2hy =

您能告诉我哪一个是正确的,以及如何获得同一件事是角度的?

谢谢

在浏览器字符串编码中通常是UTF-8,因此请使用UTF-8作为字符串编码应修复。顺便说一句,您应该明确设置两侧编码的字符串,以确保您将获得相同的结果。

var hmacsignature = crypto.createHmac('sha256', Buffer.from('123456789', 'utf8'))
.update("simple")
.digest()
.toString('base64');

new Buffer(string)被弃用,如果可以的话,请使用Buffer.from(string[, encoding])

最新更新