小编典典

如何使用bcrypt将纯文本密码与哈希密码进行比较?

python

我想用bcrypt散列密码,以后再验证提供的密码是否正确。

散列密码很容易:

import bcrypt

password = u'foobar'
password_hashed = bcrypt.hashpw(password, bcrypt.gensalt())

# then store password_hashed in a database

如何将纯文本密码与存储的哈希值进行比较?


阅读 152

收藏
2021-01-20

共1个答案

小编典典

使用py-bcrypt,您不需要单独存储盐:bcrypt将盐存储在哈希中。

您可以简单地将哈希用作盐,盐将存储在哈希的开头。

>>> import bcrypt
>>> salt = bcrypt.gensalt()
>>> hashed = bcrypt.hashpw('secret', salt)
>>> hashed.find(salt)
0
>>> hashed == bcrypt.hashpw('secret', hashed)
True
>>>
2021-01-20