在将用户密码存储到我的数据库之前,我使用以下代码来哈希(并希望使用盐)。
// 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) {
要工作,但这会在没有错误的情况下破坏我的程序。
我的问题是: 为什么有两个“空”参数?它们适用于什么? 哈希是否基于具有两个空值的代码进行盐化?
提前感谢您的帮助!
答案 0 :(得分:0)
以下语法来自(废弃?)bcrypt-nodejs模块1
bcrypt.hash(user.password, null, null, function(err, hash) {
您可以参考bcrypt模块2的文档。
确保您使用的是正确的模块。
答案 1 :(得分:0)
我使用了加密库进行散列,效果很好。这是我的代码片段
var salt = crypto.randomBytes(128).toString('base64');
var iterations = 10;
var keylen = 20;
crypto.pbkdf2(args.password, salt, iterations, keylen, function(succes, bcryptedPassword) {
console.log(bcryptedPassword.toString());
//Do actions here
});

请检查它是否对您有帮助
答案 2 :(得分:0)
bcrypt和bcrypt-nodejs之间存在差异。以下代码来自他们在npmjs.com的文档。
bcrypt.hash(myPlaintextPassword, salt, function(err, hash)
或
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash)
bcrypt.hash(myPlaintextPassword, null, null, function(err, hash)
您正在查看bcrypt的文档,而不是bcrypt-nodejs。如果您使用的是node.js,那么您很可能希望使用bcrypt-nodejs。我有多个利用其功能的项目。两个null
字段用于盐和进度: