Random Password Generator

python 3 views 2 hours, 19 minutes ago Public
Raw Download
python 2991 characters
#!/usr/bin/env python3
"""
Secure Random Password Generator
Generates cryptographically secure random passwords
"""

import secrets
import string

def generate_password(length=16, use_symbols=True, use_numbers=True, use_uppercase=True, use_lowercase=True):
    """
    Generate a secure random password
    
    Args:
        length (int): Length of the password (default: 16)
        use_symbols (bool): Include special characters
        use_numbers (bool): Include numbers
        use_uppercase (bool): Include uppercase letters
        use_lowercase (bool): Include lowercase letters
    
    Returns:
        str: Randomly generated password
    """
    characters = ''
    
    if use_lowercase:
        characters += string.ascii_lowercase
    if use_uppercase:
        characters += string.ascii_uppercase
    if use_numbers:
        characters += string.digits
    if use_symbols:
        characters += string.punctuation
    
    if not characters:
        raise ValueError("At least one character type must be selected")
    
    # Ensure password has at least one of each selected type
    password = []
    
    if use_lowercase:
        password.append(secrets.choice(string.ascii_lowercase))
    if use_uppercase:
        password.append(secrets.choice(string.ascii_uppercase))
    if use_numbers:
        password.append(secrets.choice(string.digits))
    if use_symbols:
        password.append(secrets.choice(string.punctuation))
    
    # Fill the rest randomly
    for _ in range(length - len(password)):
        password.append(secrets.choice(characters))
    
    # Shuffle to avoid predictable patterns
    secrets.SystemRandom().shuffle(password)
    
    return ''.join(password)

def generate_passphrase(word_count=4, separator='-'):
    """
    Generate a memorable passphrase using random words
    Example: correct-horse-battery-staple
    """
    # Simple word list (in production, use a larger dictionary)
    words = [
        'alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot',
        'golf', 'hotel', 'india', 'juliet', 'kilo', 'lima',
        'mike', 'november', 'oscar', 'papa', 'quebec', 'romeo',
        'sierra', 'tango', 'uniform', 'victor', 'whiskey', 'xray',
        'yankee', 'zulu', 'tiger', 'eagle', 'falcon', 'raven',
        'phoenix', 'dragon', 'wolf', 'bear', 'lion', 'panther'
    ]
    
    selected_words = [secrets.choice(words) for _ in range(word_count)]
    return separator.join(selected_words)

if __name__ == '__main__':
    print("=== Password Generator ===\n")
    
    print("Strong password (16 chars):")
    print(generate_password(16))
    
    print("\nExtra strong password (24 chars):")
    print(generate_password(24))
    
    print("\nAlphanumeric only (no symbols):")
    print(generate_password(16, use_symbols=False))
    
    print("\nMemorable passphrase:")
    print(generate_passphrase(4))
    
    print("\nGenerate 5 passwords:")
    for i in range(5):
        print(f"  {i+1}. {generate_password(16)}")