Created
February 26, 2021 19:03
-
-
Save MrZombie69232/9f99f1d8ac9708069bf64e365fb5d58e to your computer and use it in GitHub Desktop.
A python program to check your password is pwned or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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