Urgent.News

What's breaking now, across hundreds of outlets.

Finance & Markets

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.…

When users wish to determine if they have been affected by a data breach, they should not simply paste their password into a standard form that transmits the plaintext over a network to a server. Instead, a zero-knowledge password exposure checker can be built directly in the browser using the native Web Crypto API and the k-Anonymity range model.

This method does not require any external dependencies. The k-Anonymity range model functions by having the client only share a 5-character hash prefix of the password, rather than the entire hash or password itself.

First, the browser computes the SHA-1 digest of the password locally. It then splits this digest into two parts: a 5-character prefix and a 35-character suffix that remains in memory on the client's device. The prefix is then sent to https://api.pwnedpasswords.com/range/{prefix} and the server responds with numerous matching hash suffixes along with the corresponding breach counts.

Locally, the client checks if the suffix matches any of these returned suffixes. This way, the server is unaware of the specific hash or password that was checked due to the shared prefix amongst multiple hashes.

The core JavaScript implementation for this check is provided in the wire material. It includes two essential functions: `sha1Hex(secret)` to compute the SHA-1 hash of the password in hexadecimal format, and `checkPasswordExposure(password)` to perform the password exposure check. The `checkPasswordExposure` function fetches the hash range data for the 5-character prefix and searches through the responses to find a matching suffix.

If a match is found, the function returns that the password has been exposed along with the count of breaches; otherwise, it returns that the password is not exposed.

The author has also built a live demo portal, databreaches.live, which provides a user-friendly interface where developers and everyday users can safely verify their credential exposure. This portal comes with an automated breach intelligence feed. Additionally, the author has created open-source auditing utilities on GitHub (k-anonymity-credential-auditor) which can be integrated into CI/CD pipelines or terminal workflows for automated breach hygiene checks.

The article concludes with best practices for scaling client-side security tools, emphasizing the importance of not logging input states, using strict Content Security Policy (CSP) headers, encouraging password rotation, enabling two-factor authentication, and avoiding unencrypted network connections after a match is found.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Finance & Markets

More from Thursday 26 November →