我试图弄清楚如何使用加密模块在NodeJ中加密和散列密码.我可以通过以下方式创建哈希密码:
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();
});
});
然而,我对以后如何验证密码感到困惑.
UserSchema.methods.validPassword = function(password) {
// need to salt and hash this password I think to compare
// how to I get the salt?
}