通过bcrypt处理密码。当登录方法compareSync返回布尔值false

时间:2018-03-15 04:17:59

标签: javascript bcrypt

const saltRounds = bcrypt.genSaltSync(10);
var hashedPassword = bcrypt.hashSync(ctx.params.password, saltRounds);
var validPassword = bcrypt.compareSync(hashedPassword, result.password);

result.password是密码,保存在数据库中

1 个答案:

答案 0 :(得分:0)

根据documentation,您应该将纯文本密码作为第一个参数传递给bcrypt.compareSync()

const saltRounds = bcrypt.genSaltSync(10);
const plainTextPassword = ctx.params.password;
const hashedPassword = bcrypt.hashSync(plainTextPassword, saltRounds);
const valid = bcrypt.compareSync(plainTextPassword, hashedPassword);