5
我們有代碼,它適用於python 2
。BCrypt。如何用python3存儲鹽?
@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.cqlengine.ValidationError: passwd <class 'bytes'> is not a string
確定。鑄鹽和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'
我明白了。麻煩的是,該字符串轉換爲字符串,如「b」$ 2b $ 12 $ 8YtRw4YT27XpnpSBVZ9KeOlwKXdFhEMjN1Mqee6ySc7.71D1GHRKe'「'。我們應該使用'bcrypt.hashpw(passwd,bcrypt.gensalt())來代替str。解碼( 'UTF-8')'。 –
爲什麼不使用'bcrypt.checkpw()'?它看起來像實際的散列值是可變的,並不一定可比(儘管它們在同一次運行中可能是相同的)。 – Shule
@Shule:感謝您指出,'checkpw()'是在[version 3.1.0](https://github.com/pyca/bcrypt/#310)中添加的。在此之前,大概這個答案發布的時間,檢查密碼的方式如上所示。我已經更新了原始答案以推薦使用新功能。哈希改變沒有問題,因爲salt與哈希存儲在一起。如果您生成不同的鹽,那麼您將得到不同的哈希值,但這是一個不同的問題。 – mhawke