-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_checker.py
More file actions
25 lines (20 loc) · 885 Bytes
/
Copy pathpassword_checker.py
File metadata and controls
25 lines (20 loc) · 885 Bytes
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
import requests
import hashlib
def check_password(password: str) -> bool:
# Convert password to SHA1 hash
sha1pwd = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
prefix, suffix = sha1pwd[:5], sha1pwd[5:]
# Query HaveIBeenPwned API (k-anonymity)
url = f"https://api.pwnedpasswords.com/range/{prefix}"
response = requests.get(url)
if response.status_code != 200:
raise RuntimeError("Error fetching API results.")
# Check if suffix exists in response
hashes = (line.split(':') for line in response.text.splitlines())
return any(h[0] == suffix for h in hashes)
if __name__ == "__main__":
pwd = input("Enter a password to check: ")
if check_password(pwd):
print("⚠️ This password has been pwned! Choose another.")
else:
print("✅ Safe! This password was not found in breaches.")