简体   繁体   English

bcrypt 密码比较函数总是返回 false

[英]bcrypt password compare function always return false

I am using node, mysql as the server and DB and ionic on the front end.我使用 node、mysql 作为服务器,在前端使用 DB 和 ionic。 I've managed to register a user with a hash using bcrypt and attempting to authenticate the user to log them in.我已经设法使用 bcrypt 使用哈希注册用户并尝试对用户进行身份验证以登录。

When comparing the password that user enters, in bcryptJS it seems like we cannot decrypt their password.当比较用户输入的密码时,在 bcryptJS 中似乎我们无法解密他们的密码。 When I console.log password and the result of user lookup in my db, I am comparing the password that the user enters with the hash that's stored so i am always retuning a 400 status to the front end.当我 console.log 密码和用户在我的数据库中查找的结果时,我将用户输入的密码与存储的哈希值进行比较,因此我总是将 400 状态重新调整到前端。

Authentication code:验证码:

app.post('/login', function(req, res) {
  connection.connect(function(err) {
    let email = req.body.email;
    let password = req.body.password;

    connection.query("SELECT * FROM sometable WHERE username = ? ", [email], function(error, results, fields) {


       bcrypt.compare(req.body.password, results[0].password, function(err, result) {
         console.log('>>>>>> ', password)
         console.log('>>>>>> ', results[0].password)
         if(result) {
           return res.send();
         }
         else {
           return res.status(400).send();
         }
       })
    });

  });
});

What's the proper way to compare the password user enters with the hash that's stored in my db?将用户输入的密码与存储在我的数据库中的哈希值进行比较的正确方法是什么?

Thanks for your help.谢谢你的帮助。

edit:编辑:

I've tried the below code (adding a password strings) and I'm still getting the false result... What am I missing here?我已经尝试了下面的代码(添加密码字符串),但我仍然得到错误的结果......我在这里错过了什么?

 bcrypt.compare('somePassword', 'somePassword', function(err, res) {
          if(res) {
            console.log('true')
          } else {
           console.log('false')
          }
        });

Check to ensure you have the password before doing the comparison to know if the passwords match.在进行比较之前检查以确保您拥有密码以了解密码是否匹配。

see my modification below请参阅下面的我的修改

 app.post('/login', function(req, res) { connection.connect(function(err) { let email = req.body.email; let password = req.body.password; connection.query("SELECT * FROM sometable WHERE username = ? ", [email], function(error, results, fields) { if (results[0].password) { bcrypt.compare(req.body.password, results[0].password, function(err, result) { console.log('>>>>>> ', password) console.log('>>>>>> ', results[0].password) if(result) { return res.send(); } else { return res.status(400).send(); } }) } }); }); });

So, as discussed in the comments of the question, the issue turned out to be the format of the column used to store the hashed password.因此,正如问题的评论中所讨论的那样,问题原来是用于存储散列密码的列​​的格式。

If you set your column to char(50) for instance, some databases will just silently remove anything beyond 50 chars, or add spaces to get to 50 chars if you have less.例如,如果您将列设置为char(50) ,则某些数据库只会默默地删除超过 50 个字符的任何内容,或者如果您有更少的字符,则添加空格以达到 50 个字符。

This then breaks the comparison with the hashed version.这会破坏与散列版本的比较。

Sorry guys!对不起各位! i had the some proble nut it was comming from mysql it was because i had a column callded password which was in CHAR(50) so if the hash is long than to 50 char it was truncating it, whyle hashed password are very long so i have changed the field from CHAR(50) to VARCHAR(255);我有一些问题,它来自 mysql 那是因为我有一个名为 password 的列,它在 CHAR(50) 中,所以如果散列长度超过 50 个字符,它会截断它,为什么散列密码很长,所以我已将字段从 CHAR(50) 更改为 VARCHAR(255); Then everything start work fine然后一切都开始正常工作

app.post('/login', function(req, res) {
  connection.connect(function(err) {
    let email = req.body.email;
    let password = req.body.password;

    connection.query("SELECT * FROM sometable WHERE username = ? ", [email], function(error, results, fields) {
    if(error) throw error;
    else { 
        if(results.length > 0) { 
        bcrypt.compare(req.body.password, results[0].password, function(err, result) {
         if(result) {
           return res.send({ message: "Login Successful" });
         }
         else {
           return res.status(400).send({ message: "Invalid Password" });
         }
        });
    } else {
        return res.status(400).send({ message: "Invalid Email" });
    } 
    }
});
});
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM