Python 如何修复bcrypt类型错误-“;Unicode对象必须在散列之前进行编码;

Python 如何修复bcrypt类型错误-“;Unicode对象必须在散列之前进行编码;,python,python-3.x,unicode,hash,bcrypt,Python,Python 3.x,Unicode,Hash,Bcrypt,当我运行下面提供的代码时,我得到一个类型错误,它说“在散列之前必须对Unicode对象进行编码”。起初我认为这可能与input语句有关,但在尝试将密码设置为普通字符串后,它仍然不起作用。如果这是一个非常简单的解决方案,我很抱歉,但我对python是新手,在这里或任何其他网站上都找不到任何其他答案。如果这些信息对你有帮助的话,我正在使用Python3 我的代码: import bcrypt password = input("Input your desired password: ") ha

当我运行下面提供的代码时,我得到一个类型错误,它说“在散列之前必须对Unicode对象进行编码”。起初我认为这可能与input语句有关,但在尝试将密码设置为普通字符串后,它仍然不起作用。如果这是一个非常简单的解决方案,我很抱歉,但我对python是新手,在这里或任何其他网站上都找不到任何其他答案。如果这些信息对你有帮助的话,我正在使用Python3

我的代码:

import bcrypt 

password = input("Input your desired password: ")
hashedPassword = bcrypt.hashpw(password, bcrypt.gensalt())

如果有人知道如何修复此问题,请提前通知我。

这里需要一个
bytes
类型而不是
str
类型的实例。这可能会解决你的问题

import bcrypt 

password = input("Input your desired password: ")
b = password.encode('utf-8') # I just added this line
hashedPassword = bcrypt.hashpw(b, bcrypt.gensalt()) # dont forget to change "password" -> "b"
祝你好运