Password Detect

Python tutorial · PySpark.in

```import boto3
import hashlib
import io

# Configure S3 client (credentials needed)
s3 = boto3.client('s3')

bucket = 'pyspark420'
key = 'uploads/338121d7-6c39-42b4-9c5c-9bf5f9384470.txt'

# Read the file from S3
response = s3.get_object(Bucket=bucket, Key=key)
content = response['Body'].read().decode('utf-8')

# Parse passwords from file (one password per line)
passwords = [line.strip() for line in content.splitlines() if line.strip()]

print(f"Loaded {len(passwords)} passwords from S3 file\n")

# Simulated leaked password hashes (SHA-256)
LEAKED_HASHES = {
    hashlib.sha256("123456".encode()).hexdigest(),
    hashlib.sha256("password".encode()).hexdigest(),
    hashlib.sha256("qwerty".encode()).hexdigest(),
    hashlib.sha256("abc123".encode()).hexdigest(),
    hashlib.sha256("admin".encode()).hexdigest(),
}

def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()

def check_strength(password):
    score = 0
    if len(password) >= 8:
        score += 1
    if any(c.isupper() for c in password):
        score += 1
    if any(c.islower() for c in password):
        score += 1
    if any(c.isdigit() for c in password):
        score += 1
    if any(c in "!@#$%^&*()-_=+[]{};:,.<>?" for c in password):
        score += 1
    return score

def strength_level(score):
    return {
        0: "❌ Very Weak",
        1: "❌ Weak",
        2: "⚠️ Medium",
        3: "⚠️ Medium",
        4: "✅ Strong",
        5: "🔥 Very Strong"
    }[score]

def check_leak(password):
    return hash_password(password) in LEAKED_HASHES

def check_single_password(password):
    score = check_strength(password)
    leaked = check_leak(password)
   
    print(f"\n🔐 PASSWORD: {'*' * len(password)}")
    print("-" * 35)
    print(f"Length        : {len(password)} characters")
    print(f"Strength Score : {score}/5")
    print(f"Strength Level : {strength_level(score)}")
   
    if leaked:
        print("🚨 ALERT: Password found in known leaks!")
    else:
        print("✅ Password not found in known leaks")
   
    print("\n💡 Suggestions:")
    if len(password) < 8:
        print("- Increase password length")
    if not any(c.isupper() for c in password):
        print("- Add uppercase letters")
    if not any(c.islower() for c in password):
        print("- Add lowercase letters")
    if not any(c.isdigit() for c in password):
        print("- Add numbers")
    if not any(c in "!@#$%^&*" for c in password):
        print("- Add special characters")
    print("-" * 35)

def password_checker():
    # Check each password from S3 file
    for password in passwords:
        check_single_password(password)
   
    # Summary
    print(f"\n📊 SUMMARY")
    print(f"Total passwords checked: {len(passwords)}")
   
    weak_count = sum(1 for p in passwords if check_strength(p) < 3)
    leaked_count = sum(1 for p in passwords if check_leak(p))
   
    print(f"Weak passwords (< 3 score): {weak_count}")
    print(f"Leaked passwords: {leaked_count}")

password_checker()

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges