I am trying to hash my passwords in the user model with crypto.PBKDF2 but my validatePassword method is failing with the following exception
return binding.PBKDF2(password, salt, iterations, keylen, digest, callback);
This is the full error
crypto.js:562
return binding.PBKDF2(password, salt, iterations, keylen, digest, callback);
^
TypeError: Not a buffer
at TypeError (native)
at pbkdf2 (crypto.js:562:20)
at Object.exports.pbkdf2Sync (crypto.js:553:10)
at new <anonymous> (c:\Users\Joseph\news-trends\models\Users.js:25:23)
at Object.<anonymous> (c:\Users\Joseph\news-trends\models\Users.js:24:39)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (c:\Users\Joseph\news-trends\app.js:17:1)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
These are the relevant methods
UserSchema.methods.setPassword = function(password){
var self = this;
crypto.randomBytes(16, function(err, salt){
if(err){ throw err; }
self.salt = salt.toString('hex');
});
crypto.pbkdf2(password, this.salt, 1000, 64, function(err, hash){
if(err){ throw err;}
self.hash = hash.toString('hex');
});
};
UserSchema.methods.validatePassword = new function(password){
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash = hash;
};
Here is a link to the full code: Repo