Możliwe zduplikowane:
Python hashlib problem “TypeError: Unicode-objects must be encoded before hashing”Pythona 3,3 - Unicode przedmioty muszą być zakodowane przed mieszania
Oto kod Pythona 3, który wytwarza hasło solą:
import hmac
import random
import string
import hashlib
def make_salt():
salt = ""
for i in range(5):
salt = salt + random.choice(string.ascii_letters)
return salt
def make_pw_hash(pw, salt = None):
if (salt == None):
salt = make_salt() #.encode('utf-8') - not working either
return hashlib.sha256(pw + salt).hexdigest()+","+ salt
pw = make_pw_hash('123')
print(pw)
Błąd, który mi daje:
Traceback (most recent call last):
File "C:\Users\german\test.py", line 20, in <module>
pw = make_pw_hash('123')
File "C:\Users\german\test.py", line 17, in make_pw_hash
return hashlib.sha256(pw + salt).hexdigest()+","+ salt
TypeError: Unicode-objects must be encoded before hashing
Nie wolno mi zmieniać algorytmu generowania hasła, więc chcę tylko naprawić błąd, używając prawdopodobnie metody encode('utf-8')
. Jak mogę to zrobić?
Problem jest z ' '123'', który ma kodowanie. –