Polo D. Vargas
Polo D. Vargas

Reputation: 1669

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  


print(getDigest('batman'))

Upvotes: 3

Views: 779

Answers (1)

matino
matino

Reputation: 17705

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

Upvotes: 3

Related Questions