所以我有一个数据库,其中一堆散列密码存储在字符串中。现在我试着把它们拉下来,并与用户输入的纯文本密码进行比较。下面是一个例子:
import bcrypt
# at creation first:
password = u"seCr3t"
hashed_password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
print(hashed_password)
#example of it being stored in the database
hashed_password = str(hashed_password)
# first attempt:
password = u"seCrEt"
print(bcrypt.checkpw(password.encode('utf8'), hashed_password.encode('utf8')))
# -> False
# second attempt:
password = u"seCr3t"
print(bcrypt.checkpw(password.encode('utf8'), hashed_password.encode('utf8')))
# -> True
# However I get the error "Invalid Salt"
我不知道如何绕过这个错误,也没能在网上找到很多关于它的信息。任何帮助都将不胜感激。谢谢
问题是您将密码强制转换为字符串。使用decode和encode确保密码转换为字符串并使用相同的格式返回。在
我不是编码格式方面的专家,所以这可能不适用于所有情况。如果你的bytes对象中有一个不能用utf8表示的字符,它可能会导致问题。我会研究存储密码的最佳实践。也许您甚至可以使用pickle,它允许您直接存储python对象。在
相关问题 更多 >
编程相关推荐