【发布时间】:2021-04-29 23:29:42
【问题描述】:
当使用 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
【问题讨论】:
-
这不是错误中的代码。错误中的代码将
hashed_pw作为盐传递。这是一个 Unicode 字符串吗? -
@TimRoberts 我添加了失败的功能,并显示了我在 Postman 中提出的请求的图像。
-
请先提取minimal reproducible example 以确保您理解实际有问题的代码,之后才在这里提问。你可以edit你的问题来解决这个问题。
-
鉴于该代码,问题不是很明显吗?
bcrypt.hashpw的两个参数都必须是字节串。它们都需要编码。
标签: python flask unicode encode bcrypt