本文共 1210 字,大约阅读时间需要 4 分钟。
如何在数据库中存储用户密码
You don’t. You don’t store passwords in the database. You store the password hash, a string generated from the password, but from which no one can go back to the original password value.
你不知道 您不在数据库中存储密码。 您存储了密码哈希 , 密码哈希是由密码生成的字符串,但是没有人可以返回到原始密码值。
Using Node, install bcrypt
:
使用Node安装bcrypt
:
npm install bcrypt
Require it, and define the salt rounds value, we’ll use it later:
需要它,并定义盐值舍入值,稍后我们将使用它:
const bcrypt = require('bcrypt')const saltRounds = 10
Create a password hash using:
使用以下命令创建密码哈希:
const hash = await bcrypt.hash('PASSWORD', saltRounds)
where PASSWORD
is the actual password string.
其中PASSWORD
是实际的密码字符串。
If you prefer callbacks:
如果您更喜欢回调:
bcrypt.hash('PASSWORD', saltRounds, (err, hash) => { })
Then you can store the hash
value in the database.
然后,您可以将hash
值存储在数据库中。
To verify the password, compare it with the hash stored in the database using bcrypt.compare()
:
要验证密码,请使用bcrypt.compare()
将其与数据库中存储的哈希进行比较:
const result = await bcrypt.compare('PASSWORD', hash) //result is true or false
Using callbacks:
使用回调:
bcrypt.compare('somePassword', hash, (err, result) => { //result is true or false})
翻译自:
如何在数据库中存储用户密码
转载地址:http://tamgb.baihongyu.com/