Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Nodejs:在bcrypt中,它在比较密码散列时返回false_Javascript_Node.js_Bcrypt - Fatal编程技术网

Javascript Nodejs:在bcrypt中,它在比较密码散列时返回false

Javascript Nodejs:在bcrypt中,它在比较密码散列时返回false,javascript,node.js,bcrypt,Javascript,Node.js,Bcrypt,在这里,我使用bcryptjs库来加密我的密码,当我插入数据库时,它可以正常工作,但每次它都返回false来比较我在数据库中插入的相同密码。这是我的密码。。请告诉我哪里错了 这段代码用于在DB中插入哈希密码,它工作得非常完美 bcrypt.hash(insertData.Password, 10, function(err, hash) { // Store hash in your password DB. console.log('h

在这里,我使用bcryptjs库来加密我的密码,当我插入数据库时,它可以正常工作,但每次它都返回false来比较我在数据库中插入的相同密码。这是我的密码。。请告诉我哪里错了

这段代码用于在DB中插入哈希密码,它工作得非常完美

     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)                               
             }

        });
 });
当您
compare()
时,需要将明文值作为第一个参数传入,并将数据库中的哈希值作为第二个参数传入。例如:

var hashFromDB = '$2a$10$foo';
var plainPassFromUser = 'mypassword';

bcrypt.compare(plainPassFromUser, hashFromDB, function(err, matches) {
  if (err)
    console.log('Error while checking password');
  else if (matches)
    console.log('The password matches!');
  else
    console.log('The password does NOT match!');
});

compare()
之前,您也不需要第二次
bcrypt.hash()
。当您插入数据库时,只需执行一次。

您是否尝试将
compare()
'ing
actualPass
与数据库中的散列(例如
insertData.Password
)而不是新生成的散列进行比较?是的,我尝试了,这是我的代码,您可以检查它。但是我不知道它是如何工作的。@mscdex你能看看我的问题吗?这里是链接