<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: anass hanafi</title>
    <description>The latest articles on DEV Community by anass hanafi (@anass_hanafi_0fb164af6f18).</description>
    <link>https://dev.to/anass_hanafi_0fb164af6f18</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4265675%2Ff42ddfb6-3f34-4551-9cc5-f6683b2839c0.jpg</url>
      <title>DEV Community: anass hanafi</title>
      <link>https://dev.to/anass_hanafi_0fb164af6f18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anass_hanafi_0fb164af6f18"/>
    <language>en</language>
    <item>
      <title>Building a Zero-Knowledge Password Exposure Checker in Vanilla JS Using the Web Crypto API</title>
      <dc:creator>anass hanafi</dc:creator>
      <pubDate>Thu, 26 Nov 2026 00:39:59 +0000</pubDate>
      <link>https://dev.to/anass_hanafi_0fb164af6f18/building-a-zero-knowledge-password-exposure-checker-in-vanilla-js-using-the-web-crypto-api-4d8e</link>
      <guid>https://dev.to/anass_hanafi_0fb164af6f18/building-a-zero-knowledge-password-exposure-checker-in-vanilla-js-using-the-web-crypto-api-4d8e</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Fortunately, you can build a &lt;strong&gt;zero-knowledge password audit tool&lt;/strong&gt; directly in the browser using the native &lt;strong&gt;Web Crypto API&lt;/strong&gt; (&lt;code&gt;crypto.subtle&lt;/code&gt;) and the &lt;strong&gt;k-Anonymity range model&lt;/strong&gt;—with zero external dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  How the k-Anonymity Range Model Works
&lt;/h3&gt;

&lt;p&gt;Instead of transmitting a password or even its full hash over the network, the client only shares a 5-character hash prefix:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hash Locally:&lt;/strong&gt; Compute the SHA-1 digest of the password inside the browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Split the Digest:&lt;/strong&gt; Take the first 5 hex characters (&lt;code&gt;prefix&lt;/code&gt;) and keep the remaining 35 characters (&lt;code&gt;suffix&lt;/code&gt;) in local memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query the Range API:&lt;/strong&gt; Send only the 5-character prefix to &lt;code&gt;https://api.pwnedpasswords.com/range/{prefix}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare Locally:&lt;/strong&gt; The API returns hundreds of matching hash suffixes alongside breach counts. Your script checks locally if your suffix is in that list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because hundreds of hashes share the same 5-character prefix, the server never knows which exact hash—or password—you checked.&lt;/p&gt;

&lt;h3&gt;
  
  
  Minimal Client-Side Implementation
&lt;/h3&gt;

&lt;p&gt;Here is the core JavaScript logic powering the check:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
javascript
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) =&amp;gt; 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/$](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 &amp;amp; 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 &amp;amp; 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!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>security</category>
      <category>javascript</category>
      <category>cybersecurity</category>
      <category>privacy</category>
    </item>
  </channel>
</rss>
