简体   繁体   中英

Node.js crypto PBKDF2 function returns different values on v8 vs v10

This node code snippet returns different values based on the node platform version. I have two identical instances (except for the node version) in EC2 running Ubuntu 14.04.

"use strict";
var crypto = require("crypto");
crypto.pbkdf2("password", "salt", 1000, 32, function(err, derivedKey) {
if (err) {
console.error(err);
} else {
console.log(new Buffer(derivedKey).toString('base64'));
}
});    

On node v0.8.28 the console value is:

bsKIwr7Ci8KtfsKuwp3CnhDCqgYSJANPw61Iw5A/w4vCrcKWwotWAGfChFPCnVIU

On node v0.10.5 the value is:

boi+i61+rp2eEKoGEiQDT+1I0D/LrZaLVgBnhFOdUhQ=

This is a huge problem since we are upgrading the node version on the app and passwords cannot be decrypted properly.

Thanks!

It's strange how you find answers after you ask for help.

By passing the encoding type to the Buffer function like this:

console.log(new Buffer(derivedKey,'binary').toString('base64'));

...I can get the same hash.

控制台片段

It turns out the default type was changed from binary to utf8, so 'binary' needs to be specified now. https://nodejs.org/api/crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback (see bottom notes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM