bcrypt node.js(自动生成盐和散列)
在将用户密码存储到数据库之前,我使用以下代码对其进行散列(希望是salt)bcrypt node.js(自动生成盐和散列),node.js,hash,bcrypt,salt,saltedhash,Node.js,Hash,Bcrypt,Salt,Saltedhash,在将用户密码存储到数据库之前,我使用以下代码对其进行散列(希望是salt) // 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('
// 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的文档(),我期望得到以下代码
const saltrounds = 10;
bcrypt.hash(user.password, saltRounds, function(err, hash) {
正在工作,但这会毫无错误地中断我的程序
我的问题是:
为什么有两个“空”参数?它们是干什么用的?
散列是基于带有两个空值的代码进行的吗
提前感谢您的帮助 以下语法来自(废弃的?)bcrypt nodejs模块 您可以参考bcrypt模块的文档
确保您使用的是正确的模块。我使用了加密库进行哈希运算,效果非常好。这是我的代码片段
var salt=crypto.randomBytes(128).toString('base64');
var迭代次数=10;
var-keylen=20;
pbkdf2(args.password、salt、iterations、keylen、function(success、bcryptedPassword){
log(bcryptedPassword.toString());
//在这里做动作
});代码>和之间存在差异。以下代码来自他们在npmjs.com上的文档
bcrypt散列
或
bcrypt节点散列
解释
您查看的是bcrypt的文档,而不是bcrypt nodejs。如果您使用的是node.js,那么很可能需要使用bcrypt nodejs。我有多个项目利用它的功能。两个null
字段用于salt和progress:
- salt-[必需]-用于哈希密码的salt
- progress-在哈希计算期间调用的回调,以表示进度
您使用的是什么版本的nodejs和bcrypt模块?您需要指定摘要作为crypto.pbkdf2
的第五个参数,例如“sha256”
bcrypt.hash(user.password, null, null, function(err, hash) {
bcrypt.hash(myPlaintextPassword, salt, function(err, hash)
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash)
bcrypt.hash(myPlaintextPassword, null, null, function(err, hash)