为什么我通过bcrypt的哈希密码在数据库中存储时不同



我通过bcrypt加密密码,但当我在console.log中检查散列密码时,它与将存储在数据库中的散列密码不同。所以当我在登录时通过bcrypt比较密码时,显示密码不正确。

const user = require('../models/userModel');
const bcrypt = require('bcrypt');
const handelNewUser = async (req,res) => {
const {name,email,password} = req.body;
if(!name || !email || !password){return res.status(400) .json(`message:Please fill all the fields`)}
const userExist = await user.findOne({email}).exec();
if(userExist){return res.status(400).json(`message:User already exist`)}
const hash = await bcrypt.hash(password,10);
console.log(hash);
const newUser = new user({
name,
email,
password:hash
});
console.log(newUser);
newUser.save()
.then(() => {
res.json({
message:'User Created Successfully'
})
//console.log(newUser)
})
.catch(err => {
res.json({
message:err
})
})

}
module.exports = {handelNewUser};

我将在console.log中得到newUser的输出console.log输出

和存储在数据库中的newUser密码mongodb数据库中的数据

根据设计,对于相同的输入密码短语,每次运行Bcrypt都会产生不同的输出。这样,您就不能像使用过时的技术(如无盐MD5)那样为密码创建查找表。

如果它产生相同的输出,那么您的Bcrypt实现将被破坏。

最新更新