Random Password Generator

Strong passwords are essential in personal life and an enterprise environment. Having a strong password keeps us safe from unauthorized access to things such as personal banking accounts, sensitive information, and information of others depending on the work environment. Without a strong and complex password we are leaving ourselves open to the threat of cyber attackers. The script I made will help minimize the risk of password attacks to keep private information the way it should be, private. This script has defined strength characteristics that can be modified given the desire for more or less complexity. Within this password script, a password will be created and hashed for more security which will limit many possible password attacks. 

import random #Importing random module for generating random characters

import string #Importing random string module for character manipulation

import hashlib #Importing the hashlib module for password hashing

These lines import the built-in Python modules necessary for this random generation and hashing of a password. These modules will be used later in the script for random characters, allow for string manipulation, and the necessary tools for password hashing.

def generate_password(length):

This line creates a function called “generate_password” that takes one argument “length”. This length can be modified depending on the user’s desire.

all_characters = string.ascii_letters + string.digits + string.punctuation

This line creates a string that contains all letters (upper and lowercase), all digits, and all special characters. We concentrated the spring by using the ascii letters,digits, and punctuation attribute from the string module.

password = ''.join(random.choice(all_characters) for i in range(length))

Next, the random password is generated. Inside of the parentheses, we used the random.choice attribute from the random module to randomly select from all_characters. Also inside of the parentheses we used a for loop to iterate over the range of the desired length and get the correct amount of characters.

while True:

Next we will start a while True loop to make sure the generated password meets the complexity requirements.

if (

			any(c.islower() for c in password) #Check for a lowercase letter

            		and any(c.isupper() for c in password) #Check for an uppercase letter

            		and any(c.isdigit() for c in password) #Check for at least one digit

            		and any(c in string.punctuation for c in password) #Check for a character


We will start an “if” statement inside of the while loop to check the complexity rules. Inside of this “if” statement, we are checking to make sure this password has at least one lower case letter, at least one uppercase letter, at least one digit, and at least one form of punctuation. The password must contain all 4 of these which is defined by the word “and” before each one.

password = ''.join(random.choice(all_characters) for i in range(length))

This line generates a new random password with the specified length if the original generated password does not meet the complexity requirements.

return password

This returns the password if it meets the complexity requirements.

def hash_password(password):

Next, I will define another function called “hash_password” that will take “password” as an argument which has already been defined in the last function.

password_bytes = password.encode('utf-8')

This line calls the encode() method for the string “password” using UTF-8 encoding. This converts the string into a sequence of bytes. “password_bytes” will equal the result of string being converted to bytes.

hash_object = hashlib.sha256(password_bytes)

This line creates a hash using the SHA-256 hashing algorithm applied to the bytes that the password was just converted to. This will give you a 32-byte hash.

password_hash = hash_object.hexdigest()

Now using hexdigest() as part of the hashlib library, will return the hexadecimal version of the “hash_object”. By doing this, password_hash now becomes a string of hexadecimal characters so that the hash is human-readable.

password = generate_password(12)

Now we will call the generate_password function with the specified length of 12 characters. This will start to run the generate_password function so that we can get a random password.

print(f'Generated password is: {password}')

Here we are using an f-string to print the generated password for the user to see. Inside the {} what the result of the generate_password function.

hashed_password = hash_password(password)

Now we are calling the hash_password function with the generated password as the argument. This will take the password through the function and hash the password.

print(f'Hashed password is: {hashed_password}')

Lastly, we use another f-string to show the user their hashed password. Inside the {} is the result of the password being hashed.

Random Password Generator

Random Password Generator

Leave a Reply

Your email address will not be published. Required fields are marked *