import hashlib
import requests
# Function to check if a password has been leaked
def check_password_leak(password):
# Hash the password using SHA-1
sha1_password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
# The first 5 characters of the hash (for k-anonymity)
prefix = sha1_password[:5]
# The rest of the hash
suffix = sha1_password[5:]
# Query the Have I Been Pwned API with the first 5 characters
url = f'https://api.pwnedpasswords.com/range/{prefix}'
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Check if the suffix is in the response
hashes = (line.split(':') for line in response.text.splitlines())
for h, count in hashes:
if h == suffix:
return True, count
return False, 0
else:
raise Exception(f"Failed to connect to API: {response.status_code}")
# Example usage
if __name__ == "__main__":
passwords = ["password1", "password2", "password3"]
results = check_multiple_passwords(passwords)
for password, (leaked, count) in results.items():
if leaked:
print(f"Password '{password}' has been leaked {count} times.")
else:
print(f"Password '{password}' is safe (not found in known breaches).")
pip install requests
Regularly check your passwords with PassGuard to stay ahead of breaches and protect your accounts.