From 238c42bef4908662efc19468af9535bec7f57f71 Mon Sep 17 00:00:00 2001 From: AkashShaw27 <92719774+AkashShaw27@users.noreply.github.com> Date: Sun, 16 Oct 2022 19:46:46 +0530 Subject: [PATCH] Create checkpass.py --- Python/checkpass.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Python/checkpass.py diff --git a/Python/checkpass.py b/Python/checkpass.py new file mode 100644 index 0000000..59191e3 --- /dev/null +++ b/Python/checkpass.py @@ -0,0 +1,31 @@ +import requests +import hashlib +import sys +def request_api_data(query_char): + url="https://api.pwnedpasswords.com/range/" + query_char + res=requests.get(url) + if res.status_code !=200: + raise RuntimeError(f'Error fetching: {res.status_code}, check the api and try again') + return res +def get_password_leaks_count(hashes, hash_to_check): + hashes=(line.split(':') for line in hashes.text.splitlines()) + for h, count in hashes: + if h==hash_to_check: + return count + return 0 +def pwned_api_check(password): + sha1password=hashlib.sha1(password.encode('utf-8')).hexdigest().upper() + first5_char, tail=sha1password[:5],sha1password[5:] + response=request_api_data(first5_char) + return get_password_leaks_count(response,tail) + #Check password if it exists in API response +def main(args): + for password in args: + count=pwned_api_check(password) + if count: + print(f'{password} was found {count} times... You should probably change your password') + else: + print(f'{password} was NOT found. Carry on!') + return 'done!' +if __name__=='__main__': + main(sys.argv[1:])