I'm trying to store salt and hashed password before inserting each document into a collection. But on encoding the salt and password, it shows the following error:
line 26, in before_insert
document['salt'] = bcrypt.gensalt().encode('utf-8')
AttributeError: 'bytes' object has no attribute 'encode'
This is my code:
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'])
I'm using eve framework in virtualenv with python 3.4
encode-ing it? - jonrsharpedocument['salt'] = bcrypt.gensalt()it shows "in hashpw raise TypeError("Unicode-objects must be encoded before hashing") TypeError: Unicode-objects must be encoded before hashing" @jonrsharpe - DEVV911bcryptis returning abytesinstance, which cannot be encoded. It can be decoded if needed. Encoding =strtobytes, decoding =bytestostr. – What exactly is complaining with aTypeErrorwhere exactly? - deceze♦TypeErroratbcrypt.hashpwand it saysUnicode-objects must be encoded before hashing- DEVV911bcrypt.gensalt(). You definitely cannot, it's already abytesobject. Your issue is that there's somehow a unicode object inbcrypt.hashpw! - MisterMiyagi