I was born in Massachusetts and am a senior pursuing a double major in Computer Science and Data Science. Outside the classrom, I serve as the president for the Men’s Club Ultimate team at W&M.
I made this script so it can randomly generate passwords of a given length. A random seed can be given, that way someone can track the random seed and length they used for different websites, and regenerate the passwords when they forget them.
#############################################################################################
# File Name: randomPassword.py #
# Python Version: 3.8.10 #
# #
# Author: Bryce Whitney #
# Last Edit: September 15, 2021 #
# #
# Generates a random password using letters (uppercase and lowercase), numbers, and symbols #
#############################################################################################
# Required Imports
import sys
import string
import random
import argparse
def generatePassword(passwordLength=10, randomSeed=None):
"""
Generates a random password of the length provided in the command line.
If no length is given, the default is 8 characters. The user can also
pass a random seed argument so they can reproduce their results. This
way they can track the seeds they used in a seperate file and regenerate
their password when they forget it.
Arguments:
passwordLength [int] -- Length of the desired password. 10 characters by deafult.
randomSeed [int] -- The random seed to be used. None by deafult.
"""
# Create sets of characters
= string.ascii_uppercase
LETTERS_UPPER = string.ascii_lowercase
LETTERS_LOWER = string.ascii_letters
LETTERS = string.digits
NUMBERS = string.punctuation
SYMBOLS
# Create list of all the possible characters
= LETTERS + NUMBERS + SYMBOLS
CHARACTERS
# Set the random seed
random.seed(randomSeed)
# Generate the password by sampling all characters and return it
= ''.join(random.sample(CHARACTERS, passwordLength))
password return password
############################################
############### Main Method ################
############################################
if __name__ == '__main__':
# Create an ArgumentParser class object for dealing with commandline args
= argparse.ArgumentParser(
p ="Generates a random password using letters (uppercase and lowercase), numbers, and symbols.")
description
# Add an additional optional argument for the password length and random seed
"-l", "--length", default=10, type=int,
p.add_argument(help="Desired password length, 10 characters by deafult")
"-r", "--randomSeed", default=None, type=int,
p.add_argument(help="Desired random seed, None by deafult")
# Read any commandline arguements sent to the program
# NOTE: if -h or --help, the program stops here
= p.parse_args()
args
# Generate and print the random password
print(generatePassword(args.length, args.randomSeed))