Skip to content
Test if your user's password has been pwned using the API
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore init commit Apr 16, 2019
index.js implement hashing inside package for portability Apr 16, 2019


Test if your user's password has been pwned using the API

See Online

  • Works in modern browsers or in NodeJS
  • Small: ~3kb (with included fetch polyfill)
  • Quickly make your users' passwords more secure


npm install --save havetheybeenpwned

Note: If you support browsers that don't support WebCrypto, you can use a polyfill.


import pwned from "havetheybeenpwned"

pwned("hunter42").then(isPwned => {
  console.log(isPwned) // true (pwned)

That's it!

Other Implementations

Why you should use this

There are lots of massive data dumps floating around the internet of email and password combos. These dumps can easily be used by bad actors (h@ck3rz) to mass check for reused passwords on your website from your users.

While you can't completely break the practice of reused passwords, you can make sure that your users are using a password that has already been compromised. is a free service put together by Troy Hunt which provides an API to check for known compromised passwords.

This package checks that API without ever sending the full password using the "k-anonymity" model.

It builds upon Node.JS's crypto module and the WebCrypto API to create a hash of your password of which only the first 5 encoded characters are ever exposed. Check out the "How to build your own implementation" for full details.

This package should take minutes to put in your application code, but if passing a password around makes you nervous. I highly recommend implementing this yourself.

How to build your own implementation

In case you want to use this outside of NodeJS or the browser, here are some instructions (with JS examples) for checking the API yourself.

If you do implement this in another language, please open a PR and link it here.

1. Hashing the password

For the haveibeenpwned API we'll need to create a hashed version of the password in a format that the API expects.

  • It need to be SHA-1 encoded
  • And in hexidecimal format
  • And in all caps
import crypto from "crypto"

let hashed = crypto.createHash("sha1")

2. Get the "range" and "suffix"

We'll want to split the hashed password up into two parts:

  • The range is the first 5 characters of the hashed password
  • The suffix is all of the remaining characters after the 5th character
let range = hashed.slice(0, 5)
let suffix = hashed.slice(5)

3. Fetch the range

Next we're going to use the range to search for a whole bunch of possibly matching suffixes.

let response = await fetch(`${range}`)
let body = await response.text()

The body of the response will be in a text format like this:


4. Check the range for your suffix

Each line in the response is are the "suffixes" that match the "range" followed by a colon and a number of times the password has been pwned.

Search for your suffix in the list. If it is present, the password has been pwned, if it’s not the password is not known to have been pwned.

let regex = new RegExp(`^${suffix}:`, 'm')

regex.test(body) // true (pwned), false (not pwned)
You can’t perform that action at this time.