【发布时间】:2016-11-09 19:51:49
【问题描述】:
我试图在将每个文档插入集合之前存储盐和散列密码。但是在编码盐和密码时,它显示以下错误:
line 26, in before_insert
document['salt'] = bcrypt.gensalt().encode('utf-8')
AttributeError: 'bytes' object has no attribute 'encode'
这是我的代码:
def before_insert(documents):
for document in documents:
document['salt'] = bcrypt.gensalt().encode('utf-8')
password = document['password'].encode('utf-8')
document['password'] = bcrypt.hashpw(password, document['salt'])
我在 virtualenv 中使用 eve 框架和 python 3.4
【问题讨论】:
-
你试过不
encode-ing吗? -
是的,如果我只使用
document['salt'] = bcrypt.gensalt(),它会显示“in hashpw raise TypeError("Unicode-objects must be encrypted before hashing") TypeError: Unicode-objects must be encrypted before hashing”@jonrsharpe -
看起来
bcrypt正在返回一个bytes实例,无法对其进行编码。如果需要,它可以被解码。编码 =str到bytes,解码 =bytes到str。 – 究竟在哪里抱怨TypeError? -
它在
bcrypt.hashpw处显示TypeError并显示Unicode-objects must be encoded before hashing -
XYProblem? 您的问题不在于您无法对
bcrypt.gensalt()的结果进行编码。你绝对不能,它已经是一个bytes对象。你的问题是bcrypt.hashpw中有一个unicode对象!
标签: python python-3.x bcrypt