Skip to content

Instantly share code, notes, and snippets.

@MrZombie69232
Created February 26, 2021 19:03
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 MrZombie69232/9f99f1d8ac9708069bf64e365fb5d58e to your computer and use it in GitHub Desktop.
Save MrZombie69232/9f99f1d8ac9708069bf64e365fb5d58e to your computer and use it in GitHub Desktop.
A python program to check your password is pwned or not
import requests
import hashlib
import sys
# Creating a function to request api data
def request_api_data(query_char):
url = 'https://api.pwnedpasswords.com/range/'+ query_char
res = requests.get(url) # Sending request to api
if res.status_code != 200:
raise RuntimeError(f'Error fetching:{res.status_code},check the api and try again')
return res
# Function to get password count leaks
def get_password_leaks_count(hashes , hashes_to_check):
hashes = (line.split(':') for line in hashes.text.splitlines())
for h,count in hashes:
if h == hashes_to_check:
return count
return 0
# Creating a function to check if pwned
def pwned_api_check(password):
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
first5_chr,tail = sha1password[:5],sha1password[5:] # Extracting first 5 character and tail
response = request_api_data(first5_chr) # Sending request to api and storing the response
print(response)
return get_password_leaks_count(response,tail)
# function to get arguments
def main(args):
for password in args:
count = pwned_api_check(password)
if count:
print(f'{password} was found {count} times....you should change your password ')
else:
print(f'{password} was NOT found. You are good!')
return 'Done!'
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment