Created
August 27, 2017 13:48
-
-
Save victor-iyi/265e7e4f2ab98458a973447dcd283f1a to your computer and use it in GitHub Desktop.
Password hashing
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
import uuid | |
import hashlib | |
splitter = '$' | |
def hash_password(password): | |
# userid is used to generate a random number | |
salt = uuid.uuid4().hex # salt is stored in hex value | |
hash_pass = '{}{}{}'.format( | |
hashlib.sha256(salt.encode() + password.encode()).hexdigest(), | |
splitter, | |
salt) | |
## return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + '$' + salt | |
return hash_pass | |
def check_password(hashed_password, user_password): | |
# hexdigest is used as an algorithm for storing passwords | |
password, salt = hashed_password.split(splitter) | |
return password == hashlib.sha256(salt.encode() | |
+ user_password.encode()).hexdigest() | |
def start(): | |
# Enter password | |
new_pass = input('Please enter required password: ') | |
hashed_password = hash_password(new_pass) | |
# Re-enter password | |
print('hashed_password = {}'.format(hashed_password)) | |
old_pass = input('Re-enter new password: ') | |
# Password verification | |
if check_password(hashed_password, old_pass): | |
print('You entered the right password') | |
else: | |
print('Oops! Passwords do not match') | |
if __name__ == '__main__': | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment