Node.js bcrypt 比较返回 false

Node.js bcrypt compare returning false

我正在使用 Node.js 编写登录脚本并使用 bcrpyt 比较密码哈希值。使用 bcrpyt 库时,比较功能失败。但是,当我使用 bcrpytjs 库时,比较函数会成功。下面是登录功能。我已经包含了用于测试的哈希和密码。

密码:LHLiiSGd1xLg

哈希:$2y$10$J47x5GEFtmULWem2nh3YvuZaAiZyFZlyTUFV97dAx2.dyb8Yst43y

function login(email, password, callback) {

    // Require depedencies
    const bcrypt = require("bcrypt")
    const mysql = require('mysql');

    // Create our mysql connection
    const connection = mysql.createConnection({
        host: configuration.host,
        user: configuration.user,
        password: configuration.password,
        database: configuration.database
    });

    // Connect to the database
    connection.connect();

    // Create the database query
    const query = 'SELECT id, firstname, lastname, email, password FROM tblclients WHERE email = ?';

    // Query the database
    connection.query(query, [ email ], function(err, results) {

        // If we have an error
        if (err) return callback(err);

        // If we have no results
        if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));

        // Define the user object
        const user = results[0];
        console.log('Query: ', results);

        // Compare the two hashes
        bcrypt.compare(password, user.password.toString(), function(err, isMatch) {

            // If the passwords do not match
            if (err) return callback(err);
            if (!isMatch) return callback('Passwords do not match');

            // Return the user
            callback(null, {
                user_id: user.id.toString(),
                nickname: user.firstname + ' ' + user.lastname,
                email: user.email
            });
        });
    });
}

我用 2a 替换了 2y 前缀,该函数现在可以正常工作了。

user.password.toString().replace(/^$2y/, "a")