我正在调用 bcrypt.checkpw
函数来检查未加密密码是否与凭据数据库中存储的哈希密码匹配,但是却收到以下报错:
TypeError: Unicode-objects must be encoded before checking
我应该如何解决这个问题?有什么建议吗?
我已安装 python 2.7.6
和 bcrypt 3.1.1
我有以下代码:
def check_password(password, hashed_password)
if not bcrypt.checkpw(password, hashed_password):
raise InvalidCredentials("403 Forbidden")
else:
return true
并且收到以下错误:
文件 "/home/qt/virtualenv/lib/python2.7/site-packages/bcrypt/init.py",第100行,checkpw函数中, 抛出 TypeError("Unicode-objects must be encoded before checking")
TypeError: Unicode-objects must be encoded before checking
我查看了 bcrypt/__init__.py
,但不确定原因。
def checkpw(password, hashed_password):
if (isinstance(password, six.text_type) or
isinstance(hashed_password, six.text_type)):
raise TypeError("Unicode-objects must be encoded before checking")
u"foo"
,或者使用decode()
方法将字节字符串转换为Unicode字符串。Unicode字符串应该来自于HTML表单或类似的输入:这是经典(也是预期的)行为。 - Laurent LAPORTE