【问题标题】:bcrypt is not checking case sensitivity of the passwordbcrypt 不检查密码是否区分大小写
【发布时间】:2020-10-21 17:14:52
【问题描述】:

我正在使用 ExpressJS 和 MongoDB。在将密码存储到数据库之前,我使用 bcrypt 对密码进行哈希处理。 这是代码:

if (bcrypt.compare(req.body.password === result.password))

如何使它区分大小写?谢谢。

【问题讨论】:

  • 你确定吗,bcrypt.compare 区分大小写
  • 如果要将密码存储到数据库中,为什么要使用比较?如果有人想登录,通常使用比较,那么你需要将他的明文与数据库中存储的哈希进行比较

标签: node.js mongodb express bcrypt password-hash


【解决方案1】:

您的问题是您错误地使用了bcrypt.compare 函数。

来自此处找到的文档:https://www.npmjs.com/package/bcrypt

bcrypt.compare(myPlaintextPassword, hash, function(err, result) {
    // result == true
});
bcrypt.compare(someOtherPlaintextPassword, hash, function(err, result) {
    // result == false
});

这是一个完整的工作示例,其中检查了大写和小写密码:

const bcrypt = require('bcrypt');

const testPassword = '12345678abcdefg';

// generate a hash:
bcrypt.hash(testPassword, 10, function(err, hash) {
    // test a wrong password:
    const nonMatchingPassword = '12345678ABCDEFG';
    bcrypt.compare(nonMatchingPassword, hash, function(err, matches) {
        console.log('should not match:', matches);
        // test the right password:
        bcrypt.compare(testPassword, hash, function(err, matches) {
            console.log('should match:', matches);
        });
    });
});

这个例子的输出:

should not match: false
should match: true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多