简体   繁体   English

nodejs-bcrypt:比较哈希与密码总是返回不匹配

[英]nodejs - bcrypt: compare hash with password always return not match

i'm trying to use the npm package 'bcrypt' for insert crypted password during signup on my PSQL db and login a user. 我正在尝试使用npm软件包“ bcrypt”在我的PSQL数据库上注册并登录用户时插入加密密码。

The operations that i do: 我所做的操作:

1) Signup: Insert username and crypted password on my PostgreSQL db 1)注册:在我的PostgreSQL数据库中插入用户名和加密密码

createUser: function(username, password) {
        bcrypt.genSalt(saltCount, function(err, salt) {
            bcrypt.hash(password, salt, function(err, hash) {
                query = "insert query with generated crypt password";
                pool.query(query, (err, res) => {
                    console.log(err, res);
                })
            });
        });
    }

2) Login user: get inserted password and compare with crypted password on PostgreSQL db 2)登录用户:获取插入的密码并与PostgreSQL数据库上的加密密码进行比较

login: function(username, password) {
        const query = "select query for get crypt passowrd on db";
        pool.query(query, (err, res) => {
            const dbPsw = res.rows[0].hash_psw; // db password
            bcrypt.compare(password, dbPsw, function(err, result) {
                if (err)
                    console.log(err);
                else if (result)
                    console.log("password match");
                else
                    console.log("not match");
            });
        })
    }

The result of second function is always "not match". 第二个功能的结果始终是“不匹配”。

I saw on my PSQL db that the inserted password by the first function is always different event i always insert the same password to be crypted. 我在PSQL数据库上看到,第一个函数插入的密码始终是不同的事件,我总是插入相同的密码进行加密。

So my question is: How can i get always the same crypted password? 所以我的问题是:如何获得始终相同的加密密码? I'm probably doing something wrong but i follow the guide on npm site. 我可能做错了事,但我遵循npm网站上的指南。

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

 query = "insert query with generated crypt password";

That should be query = "insert query with generated hash " because bcrypt.hash() gives a hash as seen in the method parameter : function(err, hash) so this callback receives either an error or a hash 这应该是query = "insert query with generated hash ”,因为bcrypt.hash()给出了如方法参数中所示的哈希值: function(err, hash)因此此回调接收到错误或哈希值

There's an interesting question on how bcrypt compare works 关于bcrypt比较如何工作存在一个有趣的问题

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

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