简体   繁体   English

为什么 bcryptjs 比较给出 null 值,即使 hash 使用的是生成一个?

[英]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”,我也会得到nullWhat 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}`);
     }

For more details check this .有关更多详细信息,请查看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么即使我指定了值也返回null? - Why does this return a null even though I specified the value? 为什么即使两个密码字符串完全匹配,我也会从 bcrypt 比较中得到 isMatch null? - Why am I getting isMatch null from bcrypt compare even though two password string matches perfectly? 将字符串与正则表达式匹配,即使应匹配也为null - Matching a string with a regex gives null even though it should match 为什么即使我使用.each也只有一个元素触发动作? - Why is that only one element triggers the action even though I used .each? 即使存在元素,document.getElementById(..)仍为null - document.getElementById(..) gives null even though element is present 即使我传递了一个参数,为什么我的 doGet(e) function 返回 null 值? - Why is my doGet(e) function returning a null value even though i passed a parameter? 比较密码 BcryptJS - Compare passwords BcryptJS 为什么即使有onclick处理程序,“ currentTarget.onclick”的值仍为null却可以工作 - why does “currentTarget.onclick” have a null value even though there is an onclick handler and it works 为什么“null”会增加数组的长度,即使它代表空值? - Why does “null” increase length of an array even though it represents empty value? 未被捕获的TypeError:即使属性'value'不为null,也无法将其设置为null - Uncaught TypeError: Cannot set property 'value' of null even though it is not null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM