我在这里使用bcryptjs库来加密我的密码,当我将其插入到数据库中时,它可以正常工作,但每次比较我在数据库中插入的相同密码时,它都会返回false。以下是我的代码,请告诉我哪里出错了。
这段代码用于将哈希密码插入数据库中,它可以完美地工作。
bcrypt.hash(insertData.Password, 10, function(err, hash) {
// Store hash in your password DB.
console.log('hash' , hash)
insertData.Password = hash;
insertIntoDB(table,insertData,function(result){
if(result && result.length > 0){
res.json({
"status":"1",
"result":result[0]._id
});
}
});
});
这里是比较密码的代码,但它总是返回false。
var actualPass = results[0].Password //Store in DB password
bcrypt.hash(UserInputPassword, 10, function(err, hash) {
console.log('hash' , hash)
bcrypt.compare(actualPass, hash, function(err, response) {
if(err){
console.log("err",err)
}else{
console.log("response",response)
}
});
});
actualPass
与数据库中的哈希值(例如insertData.Password
)进行compare()
比较,而不是与新生成的哈希值进行比较吗? - mscdex