Advertisement
Guest User

Have I Been Pwned

a guest
Dec 7th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const sha1 = async str => {
  2.   const msgBuffer = new TextEncoder('utf-8').encode(str);
  3.   const hashBuffer = await crypto.subtle.digest('SHA-1', msgBuffer);
  4.   const hashArray = [...new Uint8Array(hashBuffer)];
  5.   const paddedHex = hashArray.map(b => b.toString(16).padStart(2, '0'));
  6.   return paddedHex.join('').toUpperCase();
  7. };
  8.  
  9. const request = hash => {
  10.     const prefix = hash.substring(0, 5);
  11.     const url = 'https://api.pwnedpasswords.com/range/' + prefix;
  12.     return {
  13.         response: fetch(url).then(r => r.text()),
  14.         hash: hash
  15.     };
  16. };
  17.  
  18. const search = o => {
  19.     const suffix = o.hash.slice(5);
  20.     return o.response.then(text => text.includes(suffix));
  21. };
  22.  
  23. const hibp = pass => sha1(pass).then(request).then(search);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement