【问题标题】:"TypeError: Unicode-objects must be encoded before hashing" even when password is encoded with bcrypt“TypeError:Unicode 对象必须在散列之前编码”即使密码是用 bcrypt 编码的
【发布时间】: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


【解决方案1】:

在 cmets 中求解。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多