Advertisement
Guest User

aaa

a guest
Mar 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import hashlib
  2. import requests
  3. import sys
  4.  
  5.  
  6. def lookup_pwned_api(pwd):
  7. """Returns hash and number of times password was seen in pwned database.
  8.  
  9. Args:
  10. pwd: password to check
  11.  
  12. Returns:
  13. A (sha1, count) tuple where sha1 is SHA1 hash of pwd and count is number
  14. of times the password was seen in the pwned database. count equal zero
  15. indicates that password has not been found.
  16.  
  17. Raises:
  18. RuntimeError: if there was an error trying to fetch data from pwned
  19. database.
  20. """
  21. sha1pwd = hashlib.sha1(pwd.encode('ascii')).hexdigest().upper()
  22. head, tail = sha1pwd[:5], sha1pwd[5:]
  23. url = 'https://api.pwnedpasswords.com/range/' + head
  24. res = requests.get(url)
  25. if res.status_code != 200:
  26. raise RuntimeError('Error fetching "{}": {}'.format(
  27. url, res.status_code))
  28. hashes = (line.split(':') for line in res.text.splitlines())
  29. count = next((int(count) for t, count in hashes if t == tail), 0)
  30. return sha1pwd, count
  31.  
  32.  
  33. def main(args):
  34. ec = 0
  35. for pwd in args or sys.stdin:
  36. pwd = pwd.strip()
  37. sha1pwd, count = lookup_pwned_api(pwd)
  38. if count:
  39. print(pwd, "was found")
  40. print("Hash {0}, {1} occurrences".format(sha1pwd, count))
  41. ec = 1
  42. else:
  43. print(pwd, "was not found")
  44. return ec
  45.  
  46.  
  47. if __name__ == '__main__':
  48. sys.exit(main(sys.argv[1:]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement