你不需要。你不在数据库中存储密码。你存储的是密码哈希值,这是一个由密码生成的字符串,但没有人可以从中返回到原始密码值。
使用Node,安装bcrypt 。
要求它,并定义盐轮值,我们以后会用到它。
const bcrypt = require('bcrypt')
const saltRounds = 10
创建一个密码哈希值
使用创建一个密码散列。
const hash = await bcrypt.hash('PASSWORD', saltRounds)
其中PASSWORD 是实际的密码字符串。
如果你喜欢回调。
bcrypt.hash('PASSWORD', saltRounds, (err, hash) => {
})
那么你可以在数据库中存储hash 值。
验证密码哈希值
要验证密码,请使用bcrypt.compare() ,将其与存储在数据库中的散列值进行比较。
const result = await bcrypt.compare('PASSWORD', hash)
//result is true or false
使用回调。
bcrypt.compare('somePassword', hash, (err, result) => {
//result is true or false
})