首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js加密比较返回false

Node.js加密比较返回false
EN

Stack Overflow用户
提问于 2020-02-18 05:05:40
回答 1查看 75关注 0票数 0

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

密码: LHLiiSGd1xLg

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-18 05:41:50

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

代码语言:javascript
复制
user.password.toString().replace(/^\$2y/, "$2a")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60270395

复制
相关文章

相似问题

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