Contraseña SALT y HASH en nodejs w / crypto
Estoy tratando de averiguar cómo eliminar y codificar una contraseña en nodejs usando el módulo criptográfico. Puedo crear la contraseña con hash haciendo esto:
UserSchema.pre('save', function(next) {
var user = this;
var salt = crypto.randomBytes(128).toString('base64');
crypto.pbkdf2(user.password, salt, 10000, 512, function(err, derivedKey) {
user.password = derivedKey;
next();
});
});
Sin embargo, estoy confundido acerca de cómo validar más tarde la contraseña.
UserSchema.methods.validPassword = function(password) {
// need to salt and hash this password I think to compare
// how to I get the salt?
}