pwnedpasswords
A dependency-free Node.js module for interfacing with the PwnedPasswords API by Troy Hunt (haveibeenpwned.com).
This module implements the new and improved version 2 of the API. This version of the API implements k-Anonymity and does NOT send even the hashed version of the passwords to third party. It only sends the first 5 characters of the SHA-1 hash. You can read more about it in the Troy Hunt's blog announcement post.
The reason I chose not to use any dependencies (e.g. request
or got
) is first, because it is a pretty simple API and can be handled easily by the built-in https
module. But mainly, it is for security reasons. Dependency chain can get deep and unpredictable. One rogue module down the chain can intercept requests and hand over passwords elsewhere.
Installation
Please review the code before use.
Install and save exact module version, to avoid installing, another, potentially vulnerable version in the future.
If you need to upgrade, again, review the code and upgrade to an exact version you have witnessed yourself.
npm install --save-exact pwnedpasswords
Usage
The module implements three interfaces. Callback, promises and CLI.
Return value is an integer with a count of input password prevalence.
Zero (0
) is returned when the password is not found.
Promises
const pwnedpasswords = require('pwnedpasswords')
pwnedpasswords('password')
.then(count => {
console.log('Password was found %d times.', count)
})
.catch(err => {
console.error(err)
})
Callback
const pwnedpasswords = require('pwnedpasswords')
pwnedpasswords('password', (err, count) => {
if (err) {
console.error(err)
}
console.log('Password was found %d times.', count)
})
CLI
It is not recommended to use this command with real passwords, as you are exposing the password to the process list and shell history. But this can be used for testing and educational scenarios or in other shell scripts.
$> ./node_modules/.bin/pwnedpasswords password
3730471
Or if installed globally, then run:
$> pwnedpasswords password
3730471
Or via npx
:
$> npx -q pwnedpasswords password
3730471
Dependencies
None
License
MIT