forked from CodecoolBase/ask-mate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
29 lines (20 loc) · 1.19 KB
/
test.py
File metadata and controls
29 lines (20 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import bcrypt
def hash_password(plain_text_password):
# By using bcrypt, the salt is saved into the hash itself
hashed_bytes = bcrypt.hashpw(plain_text_password.encode('utf-8'), bcrypt.gensalt())
return hashed_bytes.decode('utf-8')
def verify_password(plain_text_password, hashed_password):
hashed_bytes_password = hashed_password.encode('utf-8')
return bcrypt.checkpw(plain_text_password.encode('utf-8'), hashed_bytes_password)
if __name__ == '__main__':
# Test the above functions manually
original_password = 'my_very_secureP4ssword!' # From registration form
print('original_password: ' + original_password)
hashed_password = hash_password(original_password) # This shall be saved in the DB
print('hashed_password: ' + hashed_password)
user_input_password = 'Hey Siri, what is my password?' # From a login form, a mistyped input
is_matching = verify_password(user_input_password, hashed_password)
print('is_matching: ' + str(is_matching))
user_input_password = 'my_very_secureP4ssword!' # From a login form, the correct input
is_matching = verify_password(user_input_password, hashed_password)
print('is_matching: ' + str(is_matching))