首页 > 解决方案 > “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

请求 在此处输入图像描述

标签: pythonflaskunicodeencodebcrypt

解决方案


在评论中解决。

没有将哈希存储在数据库中,而是将密码存储在字符串中,这就是它给出错误的原因。


推荐阅读