-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_utils.py
39 lines (31 loc) · 1 KB
/
hash_utils.py
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
37
38
39
'''
Password Hasing Module
Help to hash hash password to enhance security
Functions:
- get_password_leaks_counts()
- sha1_hash()
'''
import hashlib
def get_password_leaks_count(hashes, hash_to_check):
'''
Parses the response from the Pwned Passwords API and returns the count of compromised passwords.
Args:
hashes (requests.Response): The response object from the API.
hash_to_check (str): The remaining part of the SHA-1 hash to check.
Returns:
int: The count of compromised passwords for the given hash.
'''
hashes = (line.split(':') for line in hashes.text.splitlines())
for a_hash, count in hashes:
if a_hash == hash_to_check:
return count
return 0
def sha1_hash(password):
'''
Generates the SHA-1 hash for a given password.
Args:
password (str): The password to hash.
Returns:
str: The SHA-1 hash of the password.
'''
return hashlib.sha1(password.encode('utf-8')).hexdigest().upper()