Reputation: 1669
Can you help me to fix this problem:
TypeError: can't concat bytes to str
I am trying to safely store hash + salt passwords.
I think the problem is that my salt is a byte object.
How can I transform it into a string?
Or is there a way to hash it better?
import base64
import hashlib
import os
def getDigest(password, salt=None):
if not salt:
salt = base64.b64encode(os.urandom(32))
digest = hashlib.sha256(salt + password).hexdigest()
return salt, digest
def isPassword(password, salt, digest):
return getDigest(password, salt)[1] == digest
print(getDigest('batman'))
Upvotes: 3
Views: 779
Reputation: 17705
You can do salt = salt.decode("utf-8")
after salt
is encoded to convert it to string.
Upvotes: 3