Skip to content

Instantly share code, notes, and snippets.

@asergi
Created November 23, 2013 14:41
Show Gist options
  • Save asergi/7615294 to your computer and use it in GitHub Desktop.
Save asergi/7615294 to your computer and use it in GitHub Desktop.
Hashing passwords with salt and testing input passwords.
#!/usr/bin/env python
# coding: utf-8
import uuid
import hashlib
import getpass
def get_hash(salt, plain_password):
return hashlib.sha256(salt.encode() + plain_password.encode()).hexdigest()
def hash_password(plain_password):
salt = uuid.uuid4().hex
encrypted_password = get_hash(salt, plain_password)
return salt + '$' + encrypted_password
def check_password(hashed_password, plain_password):
salt, encrypted_password = hashed_password.split('$')
re_encrypted_password = get_hash(salt, plain_password)
return encrypted_password == re_encrypted_password
password = getpass.getpass('Enter a password: ')
hashed_password = hash_password(password)
print('Salt + hashed password: ' + hashed_password)
re_password = getpass.getpass('Retype password: ')
if check_password(hashed_password, re_password):
print('Passwords match.')
else:
print('Sorry, passwords do not match.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment