【问题标题】:I don't know why bcrypt.compare with hashed password isn't same我不知道为什么 bcrypt.compare 与散列密码不一样
【发布时间】:2020-04-09 00:40:55
【问题描述】:

我真的不知道这个关于 bcrypt.compare 函数的问题 当有人加入我的网站时,使用哈希创建密码

const hash = await bcrypt.hash(u_password, 12);
const user = await User.create({
  u_email,
  u_password: hash,
  u_name,
  u_birth,
  u_nation,
  u_phnbr,
  u_sex,
});

在登录时我只是与使用 bcrypt.compare 进行比较

  module.exports = (passport) => {
  passport.use(new LocalStrategy({
    usernameField: 'u_email',
    passwordField: 'u_password',
  }, async (u_email, u_password, done) => {
    try {
      const exUser = await User.findOne({ where: { u_email } });
      if (exUser) {
        const result = await bcrypt.compare(u_password, exUser.u_password);

        console.log(u_password);
        console.log(exUser.u_password);

        if (result) {
          done(null, exUser);
        } else {
          done(null, false, { message: 'incorrect password.' });
        }
      } else {
        done(null, false, { message: 'non-exist user'});
      } 
    } catch (error) {
      console.log(error);
      done(error);
    }
  }));
};

当我执行代码时 只是返回不正确的密码,为什么 bcrypt.compare 返回 false? 请帮帮我

【问题讨论】:

    标签: node.js bcrypt


    【解决方案1】:

    我找到了解决方案。 因为在数据库中密码列的大小对于散列来说太小了 我在数据库中扩展密码列它可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2023-01-21
      • 2021-08-02
      • 2021-09-22
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多