#!/usr/bin/env python3 from sys import argv, stdout from hashlib import sha1 from getpass import getpass from requests import get RED = "\033[1;31m" GREEN = "\033[0;32m" RESET = "\033[0;0m" API = 'https://api.pwnedpasswords.com/range/' ROW = '{:<30}{:<10}{:<45}' HIDDEN = False def header(): print() print(ROW.format('password', 'leaked', 'sha1')) print('-' * 80) def prompt_password(): print() password = getpass('Tell me your password: ') global HIDDEN HIDDEN = True header() query(password) def query(password): password_hash = sha1(password.encode('UTF-8')).hexdigest().upper() request = password_hash[:5] response = get(API + request).text hash_searched = 'not yet' for answer in response.splitlines(): data = answer.split(':') combined_hash = request + data[0] if password_hash == combined_hash: hash_searched = int(data[1]) break if hash_searched == 'not yet': stdout.write(GREEN) else: stdout.write(RED) if HIDDEN: password = '*' * len(password) print(ROW.format(password, hash_searched, password_hash)) stdout.write(RESET) if HIDDEN: prompt_password() if __name__ == '__main__': if len(argv) < 2: prompt_password() else: header() for password in argv[1:]: query(password) print() exit(0)