ShieldX Toolly
Open a tool Donate
Home Home
Blog Blog
FAQ FAQ
Open a tool Donate
Back to tutorials
Privacy Intermediate 18 min 7 steps

Check password breaches without leaking them

Implement the k-anonymity model used by Have I Been Pwned. SHA-1 hash locally, send 5 characters, match the suffix in your browser.

Checking if a password has been leaked in a breach seems simple — just look it up. But sending your actual password to a server to check is the opposite of security. The k-anonymity model solves this: you can check breach status while revealing almost nothing.

The k-anonymity problem

k-anonymity ensures that any query you send could belong to at least k different items. For breach checking, this means the server cannot determine which specific password you are checking — only that it is one of many in a prefix range.

Step 1: Hash the password locally

We use SHA-1 (not for security, but for compatibility with the Have I Been Pwned API). The hashing happens entirely in your browser using crypto.subtle.digest().

                      async function sha1Hash(password) {
  const encoder = new TextEncoder();
  const data = encoder.encode(password);
  const hashBuffer = await crypto.subtle.digest('SHA-1', data);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  return hashArray.map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase();
}

// Example:
// sha1Hash('password') → '5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8'
                    

Step 2: Send only the first 5 characters

Instead of sending the full 40-character SHA-1 hash, we send only the first 5 characters. The server returns all hashes that start with that prefix — potentially thousands of them. The server never knows which one is yours.

                      const fullHash = '5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8';
const prefix = fullHash.slice(0, 5);   // '5BAA6'
const suffix = fullHash.slice(5);      // '1E4C9B93F3F0682250B6CF8331B7EE68FD8'

// Send only the prefix to the API
const response = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);
const data = await response.text();
                    

TipWith a 5-character hex prefix, there are 16^5 = 1,048,576 possible prefixes. Each prefix returns an average of ~475 hash suffixes. The server cannot distinguish your hash from any of those 475.

Step 3: Match the suffix locally

                      function checkSuffix(data, suffix) {
  const lines = data.split('\n');
  for (const line of lines) {
    const [hashSuffix, count] = line.trim().split(':');
    if (hashSuffix === suffix) {
      return parseInt(count, 10);
    }
  }
  return 0;
}

const breachCount = checkSuffix(data, suffix);
console.log(`Found in ${breachCount} breaches`);
                    

Step 4: Batch checking

To check multiple passwords efficiently, we group them by prefix and make one request per unique prefix.

                      async function batchCheck(passwords) {
  const hashes = await Promise.all(passwords.map(p => sha1Hash(p)));
  const byPrefix = new Map();

  for (let i = 0; i < hashes.length; i++) {
    const prefix = hashes[i].slice(0, 5);
    const suffix = hashes[i].slice(5);
    if (!byPrefix.has(prefix)) byPrefix.set(prefix, []);
    byPrefix.get(prefix).push({ index: i, suffix });
  }

  const results = new Array(passwords.length).fill(0);
  for (const [prefix, entries] of byPrefix) {
    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);
    const data = await res.text();
    for (const entry of entries) {
      results[entry.index] = checkSuffix(data, entry.suffix);
    }
  }
  return results;
}
                    

WarningAlways add a rate-limit delay between API calls. The Pwned Passwords API has a generous rate limit, but hammering it from a single IP can get you temporarily blocked.

What the server learns about you

What you sendWhat the server learns
5-char hash prefixYou are checking a password in one of ~475 possible hashes
Your IP addressYou exist (already known)
Timing of requestNothing useful
Your actual passwordNothing — it never leaves your browser

Try it yourself

Open the ShieldX Breach Checker and check "password" or "123456". You will see breach counts without your password ever leaving your tab.

Share
Put it into practice

Try the tools right now

Every concept in this tutorial is implemented in ShieldX. Open a tool, watch the network tab stay empty, and verify the security yourself.

Open the toolkit

No signup · No cookies · No cost · secure.toolly.site