Skip to content

Instantly share code, notes, and snippets.

@vinovator
Created January 26, 2016 14:16
Show Gist options
  • Save vinovator/111a59dd2aa3e48b9dc3 to your computer and use it in GitHub Desktop.
Save vinovator/111a59dd2aa3e48b9dc3 to your computer and use it in GitHub Desktop.
Best practice for securely hashing passwords
# pwdhash.py
# Python 2.7.6
import uuid
import hashlib
"""
source - http://pythoncentral.io/hashing-strings-with-python/
"""
def hash_password(password):
# uuid is used to generate a random number
salt = uuid.uuid4().hex
return hashlib.sha256(salt.encode() +
password.encode()).hexdigest() + ':' + salt
def check_password(hashed_password, user_password):
password, salt = hashed_password.split(':')
return password == hashlib.sha256(salt.encode() +
user_password.encode()).hexdigest()
new_pass = raw_input('Please enter a password: ')
hashed_password = hash_password(new_pass)
print('The string to store in the db is: ' + hashed_password)
old_pass = raw_input('Now please enter the password again to check: ')
if check_password(hashed_password, old_pass):
print('You entered the right password')
else:
print('I am sorry but the password does not match')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment