Repository navigation
Expand file tree
/
Copy pathbreach_checker.py
More file actions
54 lines (42 loc) · 1.72 KB
/
Copy pathbreach_checker.py
File metadata and controls
54 lines (42 loc) · 1.72 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Password Inspector v2.33
Copyright (c) 2025 Emmanuel Nkhoma
MIT License - See LICENSE file
"""
import hashlib
from typing import Tuple, Optional
import sys
import requests
#Breach Checker logic
def checkBreached(password: str) -> Tuple[bool, Optional[int]]:
"""
This function receives a password and evaluates if it's been breached by leveraging Have I Been Pwned
Args:
password: str
Returns: Tuple
a. is_pwned - bool: Shows if password has been breached or not.
b. breach_count - int: Indicates how many times the password has been breached.
"""
#No Password = No Breach
if not password:
return False, None
#Processing Entered Password
password_bytes = password.encode("utf-8")
password_hash = hashlib.sha1(password_bytes).hexdigest().upper()
#Prefix [first five chars] to send to HIBP, Suffix [the rest of the chars] to match with response suffixes
pw_prefix, pw_suffix = password_hash[:5], password_hash[5:]
#Attempting to Query HIBP with Password Hash Prefix
get_request_url = f"https://api.pwnedpasswords.com/range/{pw_prefix}"
try:
response = requests.get(get_request_url, timeout=5)
response.raise_for_status()
except requests.exceptions.RequestException as error:
print(f"Error querying HIBP: {error} .", file=sys.stderr)
return False, None
#split raw response from HIBP into lines and then the lines will be split by ":"
for line in response.text.splitlines():
response_suffix, breach_count = line.split(":")
#If password suffix matches response suffix, password has been breached
if response_suffix == pw_suffix:
return True, int(breach_count)
return False, None