Bcrypt无法工作,Bcrypt.compareSync总是返回false

最好的256

我面临的问题是,当在其他计算机上使用该代码时,bcrypt.comparesync可以返回true。但是,无论所比较的文本是相同还是不同,它在我的计算机上始终返回false。这是我现在遇到的某种错误,因为我的代码过去曾经可以工作,但是突然间它停止工作了。为什么?

我的代码:

const bCrypt = require('bcrypt');
var WebToken = require('jsonwebtoken');
var SecretKey = "Somesecretkey";

class ProfilesDB
{
    getLoginCredentials(request, respond){
        var username = request.body.username;
        var password = request.body.password;
 
        var sql = "SELECT password FROM restaurant_review.profile WHERE username = ?";
 
        var profileValues = [username,password];

        db.query(sql, profileValues, function(error, result) 
        {
            if(error)
            {
                throw error;
            }
            else
            {
                //console.log(result[0].password);
                const hash = result[0].password;
                var flag = bCrypt.compareSync(profileValues[1],hash);
                if (flag)
                {
                    var token = WebToken.sign(username,SecretKey);
                    respond.json({result:token});
                }
                else
                {
                    respond.json({result:"Invaild"});
                }
            }
        });
    }
    
    getAllProfiles(request, respond)
    {
        var sql = "SELECT * FROM restaurant_review.profile";
        db.query(sql, function(error, results){
            if(error)
            {
                throw error;
            }
            else
            {
                respond.json(results);
            }

        });
    }

    addProfile(request, respond)
    {
        //Creating a new profile class, calls for a new profile, to create a new "profile"
        var profileObject = new Profile(null, request.body.firstName, 
            request.body.lastName, request.body.username, request.body.password,
            request.body.email);
        //To encrypt the password
        profileObject.password = bCrypt.hashSync(profileObject.password,10);
        //Question mark is used as a place holder.
        var sql = "INSERT INTO restaurant_review.profile (firstName, lastName, username, password, email) Values(?,?,?,?,?)";
        
        var profileValues = [profileObject.getFirstName(), 
            profileObject.getLastName(), profileObject.getUsername(), 
            profileObject.getPassword(), profileObject.getEmail()];

        db.query(sql, profileValues, function(error, result){
              if(error)
             {
                 throw error;
             }
            else
             {
                 respond.json(result);
             }
         });
    }

在此处输入图片说明

Payam V

如果它可以在其他计算机上运行,​​但不能在您的计算机上运行,​​那么它可能来自bcrypt包以外的其他设备,例如,数据库未配置属性,等等。也许数据库表中的密码字段对字符数有限制,并且哈希密码超过这个号码?检查表中密码字段的类型,并确保它不是类似于varchar(x)且x值小的值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章