繁体   English   中英

我不知道为什么 bcrypt.compare 与散列密码不一样

[英]I don't know why bcrypt.compare with hashed password isn't same

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

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? 请帮我

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM