Skip to content

Instantly share code, notes, and snippets.

@tehshane
Created April 28, 2018 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tehshane/c8ab71798560045d0401e5a92b06d44b to your computer and use it in GitHub Desktop.
Save tehshane/c8ab71798560045d0401e5a92b06d44b to your computer and use it in GitHub Desktop.
Snippet to check a password against the Have I Been Pwned API
const PASSWORD = '';
const crypto = require('crypto')
const shasum = crypto.createHash('sha1')
const http = require('https');
shasum.update(PASSWORD);
const hash = shasum.digest('hex').toUpperCase();
http.get('https://api.pwnedpasswords.com/range/' + hash.substr(0,5), res => {
const chunks = [];
res.on('data', data => chunks.push(data));
res.on('end', () => {
const hashes = Buffer
.concat(chunks)
.toString()
.split('\r\n')
.filter(line => line.includes(hash.substring(6)))
.join('\n');
console.log(hashes || 'You good');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment