w3resource

Python Project - Random Password Generator Solutions and Explanations


Random Password Generator:

Generate a random password of a specified length.

Input values:
User specifies the desired password length.

Output value:
Randomly generated passwords of the specified length.

Example:

Input values:
Enter the desired password length: 12
Output value:
Generated password: Xy#7pLm$9oR5

Here are two different solutions for a "Random Password Generator" in Python. The program will allow users to specify the desired length of the password and will generate a random password of that length.

Solution 1: Basic Approach using the 'random' Module

Code:

# Solution 1: Basic Approach Using the `random` Module

import random  # Import the random module for random selection of characters

# Function to generate a random password
def generate_password(length):
    # Define possible characters for the password: lowercase, uppercase, digits, and special characters
    characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
    
    # Initialize an empty password string
    password = ""

    # Loop to generate each character of the password
    for _ in range(length):
        # Randomly select a character from the list and append to the password
        password += random.choice(characters)

    return password

# Get user input for the desired password length
password_length = int(input("Enter the desired password length: "))

# Generate the password using the specified length
generated_password = generate_password(password_length)

# Print the generated password
print(f"Generated password: {generated_password}")

Output:

Enter the desired password length: 12
Generated password: dp8P@$!5Bsg5

Explanation:

  • Uses the 'random' module to randomly select characters from a predefined string containing all possible characters (lowercase, uppercase, digits, and special characters).
  • A loop runs for the length specified by the user, adding a random character to the password string in each iteration.
  • This solution is straightforward and effective, but the randomness provided by the random module may not be secure enough for cryptographic purposes.

Solution 2: Using the ‘secrets’ Module for Better Security

Code:

# Solution 2: Using the `secrets` Module for Better Security

import secrets  # Import the secrets module for cryptographic randomness
import string   # Import the string module to get sets of characters

# Function to generate a secure random password
def generate_secure_password(length):
    # Combine all possible characters: lowercase, uppercase, digits, and special characters
    all_characters = string.ascii_letters + string.digits + string.punctuation
    
    # Generate the password using list comprehension and 'secrets.choice' for better randomness
    password = ''.join(secrets.choice(all_characters) for _ in range(length))

    return password

# Get user input for the desired password length
password_length = int(input("Enter the desired password length: "))

# Generate a secure password using the specified length
generated_password = generate_secure_password(password_length)

# Print the generated secure password
print(f"Generated password: {generated_password}")

Output:

 Enter the desired password length: 14
Generated password: /T)Gb?E9'9SoQd

Explanation:

  • Uses the 'secrets' module, which is designed for generating cryptographically strong random numbers, making it more secure for password generation.
  • Combines different character sets ('string.ascii_letters', 'string.digits', 'string.punctuation') to create a more comprehensive pool of possible characters.
  • Uses a list comprehension with 'secrets.choice()' to generate each character of the password, ensuring better randomness and security.
  • This approach is more secure and suitable for situations where password strength and security are critical.

Note:
Both solutions provide functionality to generate a random password, with Solution 1 offering a basic implementation using the random module and Solution 2 providing a more secure implementation using the secrets module.



Follow us on Facebook and Twitter for latest update.