根据文档,在使用 bcrypt 的 compare 方法时,返回结果将是一个布尔值,表示输入的密码是否匹配存储在数据库中的散列密码。在文档中提供了两个示例:一个是匹配的情况(bacon),另一个是不匹配的情况(not_bacon)。
示例代码如下:
const bcrypt = require('bcrypt'); const saltRounds = 10; const myPlaintextPassword = 'bacon'; const someOtherPlaintextPassword = 'not_bacon';
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) { // 将哈希密码存储在数据库中 });
// 在登录时使用 bcrypt.compare 进行验证 bcrypt.compare(myPlaintextPassword, hash, function(err, result) { // result 为 true 表示匹配 });
bcrypt.compare(someOtherPlaintextPassword, hash, function(err, result) { // result 为 false 表示不匹配 });