首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bcrypt.compareSync无法工作,Bcrypt总是返回false

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

Stack Overflow用户
提问于 2021-01-11 10:35:43
回答 1查看 76关注 0票数 0

我面临的问题是,当代码在其他计算机上使用时,bcrypt.comparesync可能会返回true。然而,在我的计算机上,无论被比较的文本是相同还是不同,它总是返回false。这是不是我面临的某种bug,因为我的代码在过去是可以工作的,但突然它停止了工作。为什么会这样呢?

我的代码:

代码语言:javascript
复制
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);
             }
         });
    }

enter image description here

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-11 11:01:16

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65660678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档