I'm working on a Google App Engine
application, and I need to hash a password and salt it, and for that I'm using the method below:
def hash_password(password):
#Create salt
salt = base64.urlsafe_b64encode(os.urandom(8))
salted = base64.b64encode(base64.urlsafe_b64decode(salt) + password)
hashed = hashlib.md5(salted).digest()
return base64.b64encode(hashed)
The problem is when trying to decode the salt from base64
, as the salt generated through urandom has non-ascii
characters. It results on the error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 2: ordinal not in range(128)
I can't skip the step of decoding the salt on base64
, as I already have a datastore with passwords and salts generated this way, that was populated through java code.
How can I preserve this structure, and still get a valid hashed password, that I can use to compare with the hashed passwords already on my datastore?
When I execute that same method on my terminal, using the python interpreter, it run without any issues, however when running it on GAE's dev server (locally, using the same python interpreter), it results in that error.