python - “TypeError:Unicode 对象必须在散列之前编码”即使密码是用 bcrypt 编码的
问题描述
当使用 bcrypt 进行发布请求时,我不断收到以下错误:
TypeError:Unicode 对象必须在散列之前进行编码
即使我在 utf-8 中编码了密码,如下面的代码所示:
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
任何帮助是极大的赞赏 :)
编辑:
代码失败的函数
def verifyPw(username, password):
if(not UserExists(username)):
return False
hashed_pw = users.find({
'Username':username
})[0]['password']
if(bcrypt.hashpw(password.encode('utf-8'), hashed_pw)==hashed_pw):
return True
else:
return False
解决方案
在评论中解决。
没有将哈希存储在数据库中,而是将密码存储在字符串中,这就是它给出错误的原因。
推荐阅读
- amazon-web-services - 如何自动聚合 Lambda-edge 的 cloudwatch 日志?
- single-spa - 这是正常行为吗?单spa activeWhen
- c# - 滑动刷新令牌生存期过期后注销用户
- git - Github - 创建一个分支,然后合并上游更改而不发回我的更改
- python-3.x - 触发后删除命令
- shell - 剪切文本文件并获取分隔符后的最后一个字段
- java - 试图为opengl实现模型批处理不确定为什么它没有显示任何东西
- python - Python 多处理(ForkAwareLocal,FileNotFoundError) - 对于许多进程来说,主线程过早关闭
- websocket - 使用 resgate.io (websockets) 模型“更改”不会触发带有钩子和 mobx 的 React 项目中的重新渲染
- json - 找不到类 io.confluent.kafka.serializers.json.KafkaJsonSchemaSerializer