2
我試圖存儲的鹽和在編碼的鹽和密碼將每個文件劃分成collection.But前散列密碼,則顯示以下錯誤:「字節」對象沒有屬性「編碼」
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與Python 3.4
你嘗試*不*'encode'-ING呢? – jonrsharpe
是的,如果我只是使用'document ['salt'] = bcrypt.gensalt()'它顯示「在hashpw raise TypeError(」Unicode-對象必須在散列之前編碼) TypeError:必須編碼Unicode-對象哈希之前「@jonrsharpe – DEVV911
它看起來像'bcrypt'返回一個'字節'的實例,*不能*編碼。如果需要,它可以被解碼。 Encoding ='str'爲'bytes',decode ='bytes'爲'str'。 - 究竟是在抱怨一個'TypeError'究竟在哪裏? – deceze