Created
November 23, 2013 14:41
-
-
Save asergi/7615294 to your computer and use it in GitHub Desktop.
Hashing passwords with salt and testing input passwords.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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