我们有代码,适用于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好的。将盐和盐浇铸成串:
@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'发布于 2016-03-17 12:19:48
更新
从3.1.0版本开始,bcrypt提供了方便的功能
checkpw(password, hashed_password)若要对散列密码执行密码检查,请执行以下操作。应使用这一方法,而不是:
bcrypt.hashpw(passwd_to_check, hashed_passwd) == hashed_passwd如下所示。仍然不需要单独存储散列。
首先,您不需要存储salt,因为它是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) == passwdhttps://stackoverflow.com/questions/36057308
复制相似问题