Password Strength Scanner and Breach Checker

import requests
import re
import hashlib # Import hashlib for SHA-1 hashing

def check_breach(password):
"""Check if the password has been exposed in data breaches using the HIBP API."""
sha1_password = hashlib.sha1(password.encode()).hexdigest().upper()
prefix = sha1_password[:5]
suffix = sha1_password[5:]

response = requests.get(f'https://api.pwnedpasswords.com/range/{prefix}')
if response.status_code == 200:
hashes = (line.split(':') for line in response.text.splitlines())
return any(suffix == hash_suffix for hash_suffix, count in hashes)
return False

def assess_password_strength(password):
"""Assess the strength of the provided password."""
strength = "Weak"
if (len(password) >= 12 and
re.search(r"[A-Z]", password) and
re.search(r"[a-z]", password) and
re.search(r"[0-9]", password) and
re.search(r"[!@#$%^&*()_+]", password)):
strength = "Strong"
elif len(password) >= 8:
strength = "Moderate"
return strength

def main():
password = input("Enter a password to check: ")
strength = assess_password_strength(password)
print(f"Password Strength: {strength}")

if check_breach(password):
print("This password has been found in data breaches. Consider using a different password.")
else:
print("This password has not been found in data breaches.")

if __name__ == "__main__":
main()