1import hashlib
2hash_object = hashlib.sha256(b'Hello World')
3hex_dig = hash_object.hexdigest()
4print(hex_dig)
5
1import uuid
2import hashlib
3
4def hash_password(password):
5 # uuid is used to generate a random number
6 salt = uuid.uuid4().hex
7 return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
8
9def check_password(hashed_password, user_password):
10 password, salt = hashed_password.split(':')
11 return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
12
13new_pass = input('Please enter a password: ')
14hashed_password = hash_password(new_pass)
15print('The string to store in the db is: ' + hashed_password)
16old_pass = input('Now please enter the password again to check: ')
17if check_password(hashed_password, old_pass):
18 print('You entered the right password')
19else:
20 print('I am sorry but the password does not match')
21