簡體   English   中英

Bcrypt 無法工作,Bcrypt.compareSync 總是返回 false

[英]Bcrypt unable to work, Bcrypt.compareSync always return false

我面臨的問題是,當代碼在其他計算機上使用時,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);
             }
         });
    }

在此處輸入圖像描述

如果它適用於其他計算機但不適用於您的計算機,則它可能來自 bcrypt package 以外的其他計算機,例如,數據庫未配置屬性等。可能您的數據庫表中的密碼字段對字符數有限制,並且散列密碼超過這個號碼? 檢查表中密碼字段的類型,確保它不是像 varchar(x) 這樣的 x 值很小的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM