-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswords.py
More file actions
30 lines (26 loc) · 1.11 KB
/
passwords.py
File metadata and controls
30 lines (26 loc) · 1.11 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
30
import hashlib
import string
import random
#Function that will hash the user password combined with a salt value
def hash_pwd(password):
salt = "".join(random.choices(string.hexdigits, k=32))
salted_password = (password + salt).encode("utf-8")
hash_object = hashlib.sha256(salted_password)
hashed_password = hash_object.hexdigest()
return hashed_password, salt
#Function that will check the given user password against the hashed password stored in the db
def check_pwd(password, saved_password_hash, salt):
salted_password = (password + salt).encode("utf-8")
hash_object = hashlib.sha256(salted_password)
hashed_password = hash_object.hexdigest()
return hashed_password == saved_password_hash
#Test function to test the functionality of the above functions
def test_hash_password():
hashed_password, salt = hash_pwd("password")
assert type(hashed_password) is str
assert type(salt) is str
assert check_pwd("passwordx", hashed_password, salt) == False
assert check_pwd("password", hashed_password, salt) == True
if __name__ == "__main__":
test_hash_password()
print("done")