博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在数据库中存储用户密码_如何在数据库中存储密码
阅读量:2509 次
发布时间:2019-05-11

本文共 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)

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值存储在数据库中。

验证密码哈希 (Verify the password 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/

你可能感兴趣的文章
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
docker容器秒死的解决办法
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>