admin管理员组

文章数量:1434203

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.

本文标签: javascriptHmac sha256 base64 and CryptoJS diffrent for nodejs cryptoStack Overflow