-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSystem.py
More file actions
36 lines (30 loc) · 1.35 KB
/
HashSystem.py
File metadata and controls
36 lines (30 loc) · 1.35 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
31
32
33
34
35
36
import hashlib
import os
def generateSalt():
"""
A function that generates a random salt value.
@return a random salt of 32 bytes.
"""
return os.urandom(32)
def hashPassword(password, salt):
"""
A function that generates a hashed password using a password and salt.
@param password: the password to hash.
@param salt: the salt to hash the password with.
@return the hashed password.
"""
hash_function = hashlib.sha256 # using SHA-256 hash function
hashed_password = hashlib.pbkdf2_hmac(hash_function().name, password.encode('utf-8'), salt, 100000) # hashing the password using PBKDF2
# Return the hashed password as bytes
return hashed_password
def verifyPassword(input_password, stored_salt, stored_hashed_password):
"""
A function to verify a given password, when hashed, is the same as the stored hashed password.
@param input_password: the given password to verify.
@param stored_salt: the stored salt used to create the hashed password.
@param stored_hashed_password: the stored hashed password to use to check the validity of the given password.
"""
# Hash the input password using the stored salt
hash_attempt = hashPassword(input_password, stored_salt)
# Compare the hashed input password with the stored hashed password
return hash_attempt == stored_hashed_password