Building a Zero-Knowledge Password Exposure Checker in Vanilla JS Using the Web Crypto API
When users want to check whether a password has been leaked in a data breach, pasting it into a standard form that sends the plaintext string to a backend server is a major security anti-pattern.
Fortunately, you can build a zero-knowledge password audit tool directly in the browser using the native Web Crypto API (crypto.subtle) and the k-Anonymity range model - with zero external dependencies.
How the k-Anonymity Range Model Works
Instead of transmitting a password or even its full hash over the network, the client only shares a 5-character hash prefix:
- Hash Locally: Compute the SHA-1 digest of the password inside the browser.
- Split the Digest: Take the first 5 hex characters (
prefix) and keep the remaining 35 characters (suffix) in local memory. - Query the Range API: Send only the 5-character prefix to
https://api.pwnedpasswords.com/range/{prefix}. - Compare Locally: The API returns hundreds of matching hash suffixes alongside breach counts. Your script checks locally if your suffix is in that list.
Because hundreds of hashes share the same 5-character prefix, the server never knows which exact hash - or password - you checked.
Minimal Client-Side Implementation
Here is the core JavaScript logic powering the check:
async function sha1Hex(secret) {
const data = new TextEncoder().encode(secret);
const hashBuffer = await crypto.subtle.digest('SHA-1', data);
return Array.from(new Uint8Array(hashBuffer))
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
.toUpperCase();
}
async function checkPasswordExposure(password) {
const fullHash = await sha1Hex(password);
const prefix = fullHash.slice(0, 5);
const suffix = fullHash.slice(5);
const response = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);
if (!response.ok) throw new Error('Failed to fetch range data');
const body = await response.text();
for (const line of body.split('\n')) {
const [candidateSuffix, count] = line.trim().split(':');
if (candidateSuffix === suffix) {
return { exposed: true, count: Number(count) || 1 };
}
}
return { exposed: false, count: 0 };
}
Live Demo & Automated Breach Telemetry
I wrapped this client-side logic into a fast, dark-themed security lookup portal paired with an automated breach intelligence feed so both developers and everyday users can verify their credential exposure safely:
- Live Web App: databreaches.live
- CLI & Automation Scripts: Check out my open-source auditing utilities on GitHub (k-anonymity-credential-auditor) if you want to integrate automated breach hygiene checks into your CI/CD or terminal workflows.
Best Practices When Scaling Client-Side Security Tools
- Never log input states: Keep password inputs out of analytics, session replay tools, or localStorage.
- Use strict CSP headers: Prevent third-party script injection on pages where users enter sensitive audit strings.
- Encourage credential rotation + encrypted browsing: If a match is found, users should immediately rotate reused passwords, enable hardware/app-based 2FA, and avoid logging into accounts over unencrypted public networks.
Let me know in the comments if you've implemented k-Anonymity checks in your own apps or security workflows!
Comments
No comments yet. Start the discussion.