Build a Python Breach Checker: See If Your Email Appeared in a Leak (Safely, With k-Anonymity)


Data breaches dump billions of email addresses and passwords every year. You’ve probably been told to “check if you’ve been pwned” — but pasting your password into a random website to check it is terrible advice. Today we’ll build a breach checker in Python that uses the HaveIBeenPwned API the safe way: with k-anonymity, so your password never leaves your computer.

How k-Anonymity Works

Here’s the clever trick. Instead of sending your full password to the server:

  1. Your script hashes the password with SHA-1.
  2. It sends only the first 5 characters of that hash to the API.
  3. The API returns every breached hash starting with those 5 characters — hundreds of them.
  4. Your script checks locally whether your full hash is in that list.
    The server never sees your password or your full hash, and the 5-character prefix matches so many passwords that it can’t identify yours. That’s k-anonymity: hiding in a crowd.

The Code

import hashlib
import requests
import sys
def check_password(password: str) -> int:
"""Return how many times this password appeared in breaches (0 = not found)."""
sha1 = hashlib.sha1(password.encode("utf-8")).hexdigest().upper()
prefix, suffix = sha1[:5], sha1[5:]
url = f"https://api.pwnedpasswords.com/range/{prefix}"
response = requests.get(url, timeout=10)
response.raise_for_status()
for line in response.text.splitlines():
hash_suffix, count = line.split(":")
if hash_suffix == suffix:
return int(count)
return 0
if __name__ == "__main__":
pw = input("Enter a password to check (it stays on this machine): ")
count = check_password(pw)
if count:
print(f"Found in {count:,} breaches — change it everywhere you used it.")
else:
print("Not found in known breaches. Still use a unique password per site.")

Try It

Save as breachcheck.py and run python3 breachcheck.py. Test a famously bad password like password123 — you’ll see a huge breach count. Then test a long random passphrase from your password manager and watch it come back clean.

What to Do With a Hit

  • Change it immediately, everywhere you reused it. Reuse is what turns one breach into total account takeover.
  • Turn on MFA on every important account — email first, since email resets unlock everything else.
  • Switch to a password manager so every site gets a unique, random password you never have to remember.
  • Check your email too at haveibeenpwned.com to see which breaches exposed it, and watch for phishing that uses that leaked data.

Level Up

  • Add the HIBP email API (free key) to check a list of your own addresses in bulk.
  • Wrap the script in a loop that reads passwords from your password manager’s export — locally, never uploaded.
  • Schedule a monthly check with cron so new breaches get caught early.

The Bigger Lesson

The safe way to check a secret is to never send the secret. k-anonymity is a pattern you’ll meet again in privacy engineering: share the minimum, verify locally, and trust math instead of websites. You just built a defensive tool that embodies it.

Comments

Popular Posts