Polo D. Vargas Polo D. Vargas - 2 years ago 103
Python Question

Error Hashing + Salt password

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


Answer Source

You can do salt = salt.decode("utf-8") after salt is encoded to convert it to string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download