[英]why bcryptjs compare gives null value even though hash used is generated one?
FOr authentication purpose, I'm storing the hashed password in MongoDB when I try to compare this password for the same string bcryptjs throws a null value.出于身份验证目的,当我尝试比较相同字符串的此密码时,我将散列密码存储在 MongoDB 中,bcryptjs 会抛出 null 值。
where it stores hashed password in Binary form for string '123'.它以二进制形式存储字符串“123”的哈希密码。 here is my generating code这是我的生成代码
import bcrypt
salt = bcrypt.gensalt()
password = bcrypt.hashpw(request.form['password'].encode('utf-8'), salt)
When I try to compare this with user entered password '123' I get a null value.当我尝试将此与用户输入的密码“123”进行比较时,我得到一个 null 值。
123 $2b$12$URN6pyD4SsOgIXALvr.jIuy2hvxlxva.ioamDNtMhAwvWb9/nLdhO null
here is my nodejs code with bcryptjs to compare user password with hashed database password这是我使用 bcryptjs 的 nodejs 代码,用于将用户密码与散列数据库密码进行比较
userSchema.methods.comparePassword = function (passw, cb) {
var user = this;
console.log((passw === user.password) ? 'passwords match' : 'passwords dont match' );
console.log(passw +" "+ user.password )
bcrypt.compare(passw, user.password, function (err, isMatch) {
console.log(passw +" "+ user.password +" " +isMatch )
if(err) {
return cb(err)
}
cb(null, isMatch)
})
}
I get a null value even though I enter the same string '123' also If I try checking with this online bcryptchecker website https://bcryptgenerator.com/ I get a match.即使我输入相同的字符串“123”,我也会得到null值What am I doing wrong exactly here?can someone point out my mistake?我在这里到底做错了什么?有人可以指出我的错误吗?
I think you are making the mistake in sending data to 'bcrypt.compare' method.我认为您在将数据发送到“bcrypt.compare”方法时犯了错误。 You should make changes:你应该做出改变:
bcrypt.compare(user.password, 'password from database')
.then(status => console.log(status))
.catch (e) {
console.log(`Error: ${e}`);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.