admin管理员组文章数量:1024582
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 badges1 Answer
Reset to default 1In 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.
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 badges1 Answer
Reset to default 1In 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
版权声明:本文标题:javascript - Hmac sha256 base64 and CryptoJS diffrent for nodejs crypto - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609404a2158913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论