Back to cheatsheets
Privacy
Breach Checking (k-Anonymity)
Check passwords against breach databases without leaking them using the k-anonymity protocol.
What you'll learn
- SHA-1 hash + k-anonymity
- Batch breach check
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
- Try the interactive related tool to see the concept in action.
Code Snippets
Copy-paste code snippets
2 ready-to-use snippets for breach checking (k-anonymity). Click copy and paste into your project.
SHA-1 hash + k-anonymity
async function checkBreach(password) {
const data = new TextEncoder().encode(password);
const buf = await crypto.subtle.digest('SHA-1', data);
const hash = Array.from(new Uint8Array(buf))
.map(b => b.toString(16).padStart(2, '0'))
.join('').toUpperCase();
const prefix = hash.slice(0, 5);
const suffix = hash.slice(5);
const res = await fetch(
`https://api.pwnedpasswords.com/range/${prefix}`
);
const text = await res.text();
const match = text.split('\n')
.find(l => l.split(':')[0] === suffix);
return match ? parseInt(match.split(':')[1]) : 0;
}
Batch breach check
async function batchCheck(passwords) {
const results = [];
for (const pw of passwords) {
const count = await checkBreach(pw);
results.push({ password: '***', breachCount: count });
}
return results.sort((a, b) => b.breachCount - a.breachCount);
}
Tool
Related ShieldX tool
Try this concept hands-on with the interactive tool.
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site