问题描述
我对NodeJ很新,并试图弄清楚如何使用加密模块。在玩它的同时我注意到NodeJs中的crypto模块和crypto-js之间的区别:
I'm quite new to NodeJs and trying to figure out how to use the "crypto" module. While playing around with it I notice the difference between the "crypto" module in NodeJs and crypto-js:
使用crypto-js,我有:
With crypto-js, I have:
function SHA256Hash(password, salt, iteration) {
var saltedpassword = salt + password;
var sha256 = CryptoJS.algo.SHA256.create();
for(var i = 0; i < iteration; i++) {
alert("saltedpassword = " + saltedpassword);
sha256.update(saltedpassword);
var saltedpassword = sha256.finalize();
sha256.reset();
}
return saltedpassword.toString(CryptoJS.enc.Base64);
}
然后致电:
var hashedPassword = SHA256Hash("123456789", "ASIN", 3)
和接收:
saltedpassword = ASIN123456789
saltedpassword = 3362d80b757d14bfe18c01f6a003ed38a3a4a3dcab0417efb457b71740e21411
saltedpassword = 6020c992a9b7cd3ca9e95b9a3e21b64911edb7983b3dd77bdcecda19f2756987
使用crypto模块,我写道:
With "crypto" module, I wrote:
function SHA256Hash(password, salt, iteration) {
var saltedpassword = salt + password;
for(var i = 0; i < iteration-1; i++) {
console.log("saltedpassword = "+saltedpassword)
var sha256 = crypto.createHash('sha256');
sha256.update(saltedpassword);
var saltedpassword = sha256.digest('hex');
}
console.log("saltedpassword = "+saltedpassword)
var sha256 = crypto.createHash('sha256');
sha256.update(saltedpassword);
return sha256.digest('base64');
}
然后致电:
var hashedPassword = SHA256Hash("123456789", "ASIN", 3);
并且收到:
saltedpassword = ASIN123456789
saltedpassword = 3362d80b757d14bfe18c01f6a003ed38a3a4a3dcab0417efb457b71740e21411
saltedpassword = 4795d40ae8ae797f0ce51dfe4b496bca68f6d1f4a264f4ca52348ddd65a2988d
前两项是相同的,但第三项是不同的。我错过了什么吗?
The first two items are the same but the third item is different. Did I miss out something ?
已编辑:与Jasypt相比,CryptoJs生成类似的密钥。我的问题是如何调整加密模块,使其生成与CryptoJS和Jasypt相同的密钥。
Edited: As I compare to the Jasypt, CryptoJs generates similar keys. My question is how to tune "crypto" module to make it generate the same keys as CryptoJS and Jasypt do.
推荐答案
显然我无法为怪异的答案添加评论,所以我会在这里写一下:
Apparently I can't add comments to freakish's answer, so I'll write it here instead:
reset()工作正常。重要的区别是你在迭代循环中将散列输出转换为十六进制字符串。
reset() works fine. The significant difference is you're converting the hash output to a hex string within the iteration loop.
这篇关于nodejs crypto module vs crypto-js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!