Skip to content

Instantly share code, notes, and snippets.

@andygock
Created June 14, 2024 22:11
Show Gist options
  • Save andygock/c1c4fac1ee228eff765612502ad992a7 to your computer and use it in GitHub Desktop.
Save andygock/c1c4fac1ee228eff765612502ad992a7 to your computer and use it in GitHub Desktop.
import getpass
import hashlib
import requests
# Function to get user password securely
def get_password():
password = getpass.getpass("Enter password: ")
return password
# Get the password from the user
password = get_password()
# Calculate the SHA-1 hash of the password
sha1 = hashlib.sha1()
sha1.update(password.encode("utf-8"))
hash = sha1.hexdigest().upper()
prefix = hash[:5]
suffix = hash[5:]
# Query the Pwned Passwords API
try:
response = requests.get(f"https://api.pwnedpasswords.com/range/{prefix}")
response.raise_for_status()
except requests.exceptions.RequestException as err:
print(f"Error querying the Pwned Passwords API: {err}")
exit()
# Check if the password hash suffix exists in the response
matches = [line for line in response.text.split("\n") if line.startswith(suffix)]
# Display the result to the user
if matches:
count = matches[0].split(":")[1].rstrip()
print(
f"This password has been found {count} times in data breaches. It is highly recommended to change it immediately!"
)
else:
print("This password has not been found in any known data breaches.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment