Download our e-book of Introduction To Python
Shashank Shanu
2 years ago
import uuid
import hashlib
def hash_password(password):
# userid is used to generate a random number
salt = uuid.uuid4().hex #salt is stored in hexadecimal value
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
def check_password(hashed_password, user_password):
# for storing passwords hexdigest is used as an algorithm
password, salt = hashed_password.split(':')
return password == hashlib.sha256(salt.encode()
+ user_password.encode()).hexdigest()
new_pass = input('Hello User, Please enter required password ')
hashed_password = hash_password(new_pass)
print('String to store in the db is: ' + hashed_password)
old_pass = input('Please, Re-enter new password ')
if check_password(hashed_password, old_pass):
print('You entered the right password')
else:
print('Sorry but the password does not match')
Hello User, Please enter required password 123
String to store in the db is: 24391247aaaaa26070f9b5f1f06c4526f9819ad511d2356ebb75ad9037b5baa8:e88744d2fda34e3b9b0d76c35bf049b1
Please, Re-enter new password 234
Sorry but the password does not match
Hello User, Please enter required password 123
String to store in the db is: a53693b8e064159fc8ad2926405fa7ac878890072c75d6415b5c1eae772a3198:46c3623f467b42f69fb19e668f491214
Please, Re-enter new password 123
You entered the right password