-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crypto.pbkdf2Sync return different result that previous node versions #7464
Comments
The reason for this is that the default string encoding changed (from var salt_saved = new Buffer('fhPctjE477+92KQi77+977+9dBYr77+9', 'base64').toString(); to: var salt_saved = new Buffer('fhPctjE477+92KQi77+977+9dBYr77+9', 'base64'); you will see the same (yet still different than |
I have the same issue this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
crypto.pbkdf2Sync(passwd, this.salt, 10000, 512, 'sha512').toString('base64'); // this is the the string I save in DB as password when an user try to login I hash this password with the "this.salt" iI stored in the user db object and it doesn't work anymore I update to node 6.9.2 from node 0.10 |
That's a no-op, its the same as The docs don't mention that default salt encoding changed, they just mention password, should be fixed. They also don't help much in modernizing code. @blazekas @cperezvinsite You probably figured it out, but it wasn't obvious to me, so for anyone who runs up against this, the way to convert code that passes strings as either salt or password so it will work on node >= 0.10:
But node0.10 is obsolete, if you just want code that is as safe as possible, and works identically to 0.10, and runs on node >= 4.x, use Buffer.from:
|
@sam-github thanks for the answer but still doesn't works, I will describe my problem
this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
this.passwd = crypto.pbkdf2Sync(passwd, this.salt, 10000, 512, 'sha512').toString('base64'); I saved this as an user, then when n user try to login into the system I get the password from the request and execute this function var hashPassword = function(passwd) {
return crypto.pbkdf2Sync(passwd, this.salt, 10000, 512, 'sha512').toString('base64');
};
var authenticate = function(passwd) {
return this.passwd === this.hashPassword(passwd);
}; if is true means the password is correct. My problem is, if i created an user using node 0.10 the user login only works in node 0.10, if I create the user in newest versions, it doesn't works in node 0.10, so, I'm sure the problem is in this function crypto.pbkdf2Sync(passwd, this.salt, 10000, 512, 'sha512').toString('base64'); |
To help, I need a small standalone code reproduction, with hard-coded expected input and output, like the original post (which I did get working by constructing Buffer using the old default string encoding). |
@sam-github look my test code looks like this var crypto = require('crypto');
var passwd = 'iamatest';
var salt = 'iamasaltfortest'
console.log(crypto.pbkdf2Sync(passwd, salt, 10000, 512, 'sha512').toString('base64')); with node v6.9.2 the output is (with node 4.8.2 i have the same result) with node v0.10.25 |
Node v0.10 doesn't support a custom digest, it always uses SHA-1. |
Thanks, I solved my problem changing this code crypto.pbkdf2Sync(passwd, salt, 10000, 512, 'sha512').toString('base64') to crypto.pbkdf2Sync(passwd, new Buffer(this.salt,'binary'), 10000, 512).toString('base64'); |
But note that you'll need to explicitly specify SHA-1 in newer versions of node: #11305 |
Thanks cperezvinsite, i solved too: |
Hello Friend , TypeError: The "digest" argument is required and must not be undefined Please help me , |
You have to give the 5th argument as 'sha1'. please specify a digest explicitly. |
crypto.pbkdf2Sync in node version 6 returns different result than in previous node versions.
In the code below "password_saved" should match "password_verify" (it does match in node versions prior to 6) -
The text was updated successfully, but these errors were encountered: