Advertisement
Python253

random_password_generator

May 15th, 2024
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: random_password_generator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script generates random passwords by combining words, numbers, and special characters.
  10.    It prompts the user to input a list of easily remembered words, which are then used to create a base password.
  11.    The script randomly selects characters from the words, converts some to uppercase, and inserts random numbers
  12.    and special characters at random positions to enhance password complexity.
  13.  
  14. Functions:
  15.    - generate_random_strings(word_list):
  16.        Generate two random strings from the provided list.
  17.    - generate_random_numbers():
  18.        Generate two random numbers between 0 and 9.
  19.    - generate_random_special_chars():
  20.        Generate two random special characters from a predefined list.
  21.    - generate_random_index(indices_list):
  22.        Generate a random index from the provided list of indices.
  23.    - main():
  24.        Main function to execute the password generation process.
  25.  
  26. Requirements:
  27.    - Python 3.x
  28.  
  29. Usage:
  30.    1. Run the script: python random_password_generator.py
  31.    2. Enter a list of easily remembered words separated by spaces when prompted.
  32.    3. The script will generate and display a random password combining words, numbers, and special characters.
  33.  
  34. Example Output:
  35.  
  36.    Input a list of words separated by spaces: Randomly Generated Password Example
  37.  
  38.    Generated password: E#xaMpleR:and7o3mlY
  39.  
  40. Additional Notes:
  41.    - The script combines user-provided words to create a base password, ensuring it is memorable yet secure.
  42.    - Random characters are selected from the words, and some are converted to uppercase for added complexity.
  43.    - Random numbers and special characters are inserted at random positions to enhance password strength.
  44. """
  45.  
  46. # Importing the random module
  47. import random
  48.  
  49. def generate_random_strings(word_list):
  50.     """
  51.    Generate two random strings from the provided list.
  52.  
  53.    Parameters:
  54.        word_list (list): List of strings to choose from.
  55.  
  56.    Returns:
  57.        tuple: Two randomly chosen strings.
  58.    """
  59.     string1 = random.choice(word_list)  # First random string
  60.     word_list.remove(string1)           # Remove to avoid repetition
  61.     string2 = random.choice(word_list)  # Second random string
  62.  
  63.     return string1, string2
  64.  
  65. def generate_random_numbers():
  66.     """
  67.    Generate two random numbers between 0 and 9.
  68.  
  69.    Returns:
  70.        tuple: Two randomly chosen numbers.
  71.    """
  72.     numbers = list(range(10))
  73.     number1 = random.choice(numbers)  # First random number
  74.     numbers.remove(number1)           # Remove to avoid repetition
  75.     number2 = random.choice(numbers)  # Second random number
  76.  
  77.     return number1, number2
  78.  
  79. def generate_random_special_chars():
  80.     """
  81.    Generate two random special characters from a predefined list.
  82.  
  83.    Returns:
  84.        tuple: Two randomly chosen special characters.
  85.    """
  86.     special_chars = ['~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '?', '+', '-', '_', ';', ':', '>', '<']
  87.     char1 = random.choice(special_chars)  # First random character
  88.     special_chars.remove(char1)           # Remove to avoid repetition
  89.     char2 = random.choice(special_chars)  # Second random character
  90.  
  91.     return char1, char2
  92.  
  93. def generate_random_index(indices_list):
  94.     """
  95.    Generate a random index from the provided list of indices.
  96.  
  97.    Parameters:
  98.        indices_list (list): List of indices to choose from.
  99.  
  100.    Returns:
  101.        int: A randomly chosen index.
  102.    """
  103.     index = random.choice(indices_list)
  104.     indices_list.remove(index)
  105.  
  106.     return index
  107.  
  108. def main():
  109.     # Take input from the user
  110.     words = input('Input a list of words separated by spaces: ').split()
  111.  
  112.     # Create password using two random strings
  113.     string1, string2 = generate_random_strings(words)
  114.     password = string1 + string2
  115.  
  116.     # Uppercasing random characters in the password
  117.     index_list = list(range(len(password)))
  118.     for _ in range(2):  # Perform twice
  119.         index = generate_random_index(index_list)
  120.         password = password[:index] + password[index].upper() + password[index+1:]
  121.  
  122.     # Adding random numbers to the password
  123.     number1, number2 = generate_random_numbers()
  124.     for number in (number1, number2):
  125.         index = generate_random_index(index_list)
  126.         password = password[:index] + str(number) + password[index:]
  127.  
  128.     # Adding random special characters to the password
  129.     char1, char2 = generate_random_special_chars()
  130.     for char in (char1, char2):
  131.         index = generate_random_index(index_list)
  132.         password = password[:index] + char + password[index:]
  133.  
  134.     # Printing the generated password
  135.     print('\nGenerated password:', password)
  136.  
  137. if __name__ == '__main__':
  138.     main()
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement