簡體   English   中英

Node.js bcrypt compare 返回 false 以獲得正確的密碼

[英]Node.js bcrypt compare returns false for correct password

我正在使用 bcrypt 來散列和比較用戶密碼,但是在我注冊一個新用戶然后嘗試登錄后,即使密碼正確,bcrypt 比較函數也會返回 false。

1)創建一個新用戶

function NewUser(request, reply) {
    let e = decodeURIComponent(request.params.q_email)
    let p = decodeURIComponent(request.params.q_password)

    dbCheckUserExists(e,
    (yes) => {
        return reply("User already exists")
    },
    (no) => {
        bcrypt.hash(p, 3, (err, hash) => {
            if (err) {
                return reply("Error creating new user")
            } else {
                dbCreateUser(request, reply, e, hash)
            }
        });
    });
}

function dbCreateUser(request, reply, email, pwdHash) {
    var sql = "INSERT INTO Users(Version, Email, Password, Balance) VALUES (?,?,?,?)"
    var args = [1, email, pwdHash, 0]
    sql = mysql.format(sql, args)
    executeSql(sql,
        (err, rows, fields) => {
            if (err) {
                return reply("Error creating new user")
            } else {
                return reply("Successfully created new user")
            }
        }
    );
}

2) 登錄

function dbLogin(request, reply, yes, no) {
    let e = decodeURIComponent(request.payload.q_email)
    let p = decodeURIComponent(request.payload.q_password)
    //reply('email: ' + e + ' password: ' + p)

    var sql = "SELECT Password FROM Users WHERE Email = ? LIMIT 1"
    sql = mysql.format(sql, e)

    executeSql(sql,
        (err, rows, fields) => {
            if (err) {
                throw err
            } else {
                if (rows.length == 0) {
                    //no()
                    reply("email not found")
                } else {
                    bcrypt.compare(p, rows[0].Password, (err, res) => {
                        if (res == true) {
                            reply("correct password")
                            //dbCreateSession(request, reply, yes, no)
                        } else if (res == false){
                            reply("incorrect password: " + p + " " + rows[0].Password)
                        }
                        else {
                            //no()
                            reply("neither true nor false")
                        }
                    });
                }
            }
        }
    );
}

我用電子郵件“hello”和密碼“world”創建了一個用戶並運行以下查詢

SELECT Email, Password FROM `Users` WHERE Email = 'hello'

返回以下內容

hello   $2a$04$JwaMtM577eqLRNd0m5tbTewP1IxBMSAwyW9kczPjOPjDgu9I

但是,當我嘗試登錄時,我得到以下信息(自定義響應)

incorrect password: world $2a$04$JwaMtM577eqLRNd0m5tbTewP1IxBMSAwyW9kczPjOPjDgu9I

誰能看到我哪里出錯了?

我盯着屏幕太久了!

問題是數據庫中的密碼字段被截斷(55 個字符而不是 60 個字符)

增加數據庫中密碼字段的大小,即

varchar(125)

也許你最終得到了一個無效的散列,嘗試用 bcrypt 生成散列:

bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
  // Store hash in your password DB. 
});

然后,您可以嘗試以簡單的方式檢查您在數據庫中的哈希值是否與您將使用的輸入的硬編碼版本匹配(密碼變量: p作為字符串'world'

bcrypt.compare('world', hash, function(err, result) {
 if (err) { throw (err); }
 console.log(result);
});

如果它有效(它可能會),然后嘗試對來自請求的輸入執行相同的操作。

您應該更深入地了解出了什么問題。

暫無
暫無

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

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