问题描述
我在使用 bcrypt
散列后保存我的密码现在我需要从散列值中获取原始数据。
假设,
我的密码 123456。我在哈希后保存密码假设 3jhjhjkjk34k34k34j
我需要从哈希值中获取我的密码。
解决方法
您不能使用 bcrypt 解密密码,因为它是一种单向算法。您可以做的是提供明文和哈希(来自数据库)并进行比较。
示例:
const passwordEnteredByUser = "123456"
const hash = "HASH_STRING"
bcrypt.compare(passwordEnteredByUser,hash,function(err,isMatch) {
if (err) {
throw err
} else if (!isMatch) {
console.log("Password doesn't match!")
} else {
console.log("Password matches!")
}
})