【发布时间】:2017-06-28 08:21:20
【问题描述】:
在将用户密码存储到数据库之前,我正在使用以下代码对用户密码进行哈希处理(并希望对其进行加盐处理)。
// hash the password before the user is saved
ConsultantSchema.pre('save', function(next) {
var user = this;
// hash the password only if the password has been changed or user is new
if (!user.isModified('password')) return next();
// generate the hash
bcrypt.hash(user.password, null, null, function(err, hash) {
if (err) {
logger.error("bcrypt.hash "+err);
return next(err);
}
// change the password to the hashed version
user.password = hash;
next();
});
});
让我感到困惑的是部分
bcrypt.hash(user.password, null, null, function(err, hash) {
我从教程中得到了这段代码,我经常看到它在寻找答案。 根据 bcrypt 的文档 (https://www.npmjs.com/package/bcrypt),我预计会有以下代码
const saltrounds = 10;
bcrypt.hash(user.password, saltRounds, function(err, hash) {
正在工作,但这会在没有错误的情况下破坏我的程序。
我的问题是: 为什么有两个“空”参数?它们是干什么用的? 哈希是根据两个空值的代码加盐的吗?
提前感谢您的帮助!
【问题讨论】:
-
你用的是什么版本的nodejs和bcrypt模块?
标签: node.js hash bcrypt salt saltedhash