最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hmac sha256 base64 and CryptoJS diffrent for nodejs crypto - Stack Overflow

programmeradmin1浏览0评论

Let say we have simple msg and key:

message = 'simple'

private_key = '123456789'; Using that in angular project with CryptoJS:

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

result for me is:

lvs7rQTe1EDTLAS1GVWWsNG5ZaYVCh9aaYc+NoEunC4=

using that in msg and key in node:

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

result is:

nYu2PGqfRDWnHbT649q0gc+7DcIq8iwcwHAQQa5T2HY=

Can you tell me which one is correct and how to get same thing is angular?

Thanks

Let say we have simple msg and key:

message = 'simple'

private_key = '123456789'; Using that in angular project with CryptoJS:

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

result for me is:

lvs7rQTe1EDTLAS1GVWWsNG5ZaYVCh9aaYc+NoEunC4=

using that in msg and key in node:

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

result is:

nYu2PGqfRDWnHbT649q0gc+7DcIq8iwcwHAQQa5T2HY=

Can you tell me which one is correct and how to get same thing is angular?

Thanks

Share Improve this question asked Feb 24, 2018 at 15:48 Adam AdamskiAdam Adamski 7673 gold badges11 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

In browser string encoding usually is UTF-8, so use UTF-8 as string encoding should fix it. BTW, you should explicitly set string encoding in both side to make sure you will get same result.

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

And new Buffer(string) is deprecated, use Buffer.from(string[, encoding]) if you can.

发布评论

评论列表(0)

  1. 暂无评论