在我的local-register
我将用户数据存储在数据库中。为了对密码进行哈希处理,我使用 bcrypt:
console.log(password);
password = bcrypt.hashSync(password);
console.log(password);
如果我使用密码 stackoverflow
注册用户密码如下所示:
stackoverflow
$2a$10$uoJH1Wo9b7SQploRptfODe1Q2kRC3skQoUNOIhAmHg2AWykWQwGvW
当我以用户 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8c8b9e9c9490899a8d99939088bf8c8b9e9c9490899a8d99939088d19c9092" rel="noreferrer noopener nofollow">[email protected]</a>
登录时密码 stackoverflow
var hashedpassword = bcrypt.hashSync(password);
console.log(password);
console.log(hashedpassword);
stackoverflow
$2a$10$aq869JEMWBQ8vCfXfuRvlOPdUvq.UhTz4Ge.kB3n7wSyvhjBsm8r2
因此,即使我使用相同的 bcrypt 模块,每次登录时的哈希值都是不同的。
最佳答案
我不明白 bcrypt 的内部工作原理,但每次同一个字符串的哈希值可能看起来不同,因为盐是作为哈希值的一部分生成的。因此,你不能这样做:
stored = hash(old);
guess = hash(guess);
valid = stored == guess;
相反,您必须使用 compare
方法,如下所示:
/* password is *not* hashed! */
bcrypt.compare(password, usersHashedPassword, cb);
关于node.js - Passport bcrypt 密码与 db 的密码不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39019607/