python - 加密。如何用python3存储盐?

标签 python python-2.7 python-3.x bcrypt

我们有适用于 python 2 的代码。

@password.setter
def password(self, value):
    self.salt = bcrypt.gensalt()
    self.passwd = bcrypt.hashpw(value.encode('utf-8'), self.salt)

def check_password(self, value):
    return bcrypt.hashpw(value.encode('utf-8'), self.salt.encode('utf-8')) == self.passwd

但是,当我尝试将其转换为 python3 时,我们遇到了以下问题:

错误一发生在cassandra驱动层:

cassandra.cqlengine.ValidationError: passwd <class 'bytes'> is not a string

好的。将 salt 和 passwd 转换为字符串:

@password.setter
def password(self, value):
    salt = bcrypt.gensalt()
    self.salt = str(salt)
    self.passwd = str(bcrypt.hashpw(value.encode('utf-8'), salt))

现在省盐了。但是在 check_password 中,我们得到了 ValueError: Invalid salt。 如果我们将检查密码代码更改为:

def check_password(self, value):
    return bcrypt.hashpw(value, self.salt) == self.passwd

我们收到错误 TypeError: Unicode-objects must be encoded before hashing

去哪里挖?

UPD 密码中的盐值和检查密码看起来相同,例如:

b'$2b$12$cb03angGsu91KLj7xoh3Zu'                                                                        
b'$2b$12$cb03angGsu91KLj7xoh3Zu'

最佳答案

更新

从版本 3.1.0 开始,bcrypt 提供了方便的功能

checkpw(password, hashed_password)

根据散列密码执行密码检查。这应该用来代替:

bcrypt.hashpw(passwd_to_check, hashed_passwd) == hashed_passwd

如下所示。仍然不需要单独存储散列。


首先,您不需要存储盐,因为它是 bcrypt.hashpw() 生成的散列的一部分。您只需要存储哈希。例如

>>> salt = bcrypt.gensalt()
>>> salt
b'$2b$12$ge7ZjwywBd5r5KG.tcznne'
>>> passwd = b'p@ssw0rd'
>>> hashed_passwd = bcrypt.hashpw(passwd, salt)
b'$2b$12$ge7ZjwywBd5r5KG.tcznnez8pEYcE1QvKshpqh3rrmwNTQIaDWWvO'
>>> hashed_passwd.startswith(salt)
True

所以你可以看到盐包含在散列中。

您还可以使用 bcrypt.hashpw() 来检查密码是否与散列密码匹配:

>>> passwd_to_check = b'p@ssw0rd'
>>> matched = bcrypt.hashpw(passwd_to_check, hashed_passwd) == hashed_passwd
>>> matched
True
>>> bcrypt.hashpw(b'thewrongpassword', hashed_passwd) == hashed_passwd
False

无需单独存放盐。


所以您可以像这样编写 setter(Python 3):

@password.setter
def password(self, passwd):
    if isinstance(passwd, str):
        passwd = bytes(passwd, 'utf-8')
    self.passwd = str(bcrypt.hashpw(passwd, bcrypt.gensalt()), 'utf8')

检查器是这样的:

def check_password(self, passwd_to_check):
    if isinstance(passwd_to_check, str):
        passwd_to_check = bytes(passwd_to_check, 'utf-8')
    passwd = bytes(self.passwd, 'utf8')
    return bcrypt.hashpw(passwd_to_check, passwd) == passwd

关于python - 加密。如何用python3存储盐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057308/

相关文章:

python - 当函数使用 lambda 参数时如何使用模拟

python - 此代码是否有单行解决方案

python - 我可以在 def __init__ 中添加方法吗?

Python - 替换套接字库中由另一个库调用的函数,该函数由另一个库调用

python - 使用 numpy.save (和 savez)出现意外类型错误

python - 三元表示中的快速数字总和(Python)

python - 局部模型未完全优化不规则状态

python - 如何在 Matplotlib 中选择具有日期时间坐标的点?

python - 具有 Lambda 值的字典更新所有条目

python - 在 Python 3.6 中导入库 "modin"时出错