Building a Password Generator with Python

Building a Password Generator with Python

Introduction

A password generator is a software that performs the task of generating good passwords for a user. It considers the criteria for a healthy password and gives the users the output of a suitable password that cannot easily be hacked.

A recent research carried out by thrivemyway discovered that 80% of breached accounts occur due to password hacking. It also discovered that it takes a hacker approximately 10 minutes to figure out a 6-character password.

A password generator eliminates those risks. It performs an easy creation of a long and complex password and allows users to use different passwords for different web accounts so that if one account gets compromised, the others can be safe.

About the Project

In this tutorial, we will be building two types of password generators.

  1. An easy password generator that prints out characters in a particular sequence e.g MuJA!&21

  2. A hard password generator that prints out characters in a random reshuffled sequence e.g 2M$uj!1A

Overview of Password Strength

In simple terms, password strength refers to how long it would take anyone to guess your password. The strength of your password determines if it is easy or difficult to hack. A strong password is one which is hard to guess and could take centuries to guess. On the other hand, a weak password is one which can be disclosed under a few trials.

Some guidelines for choosing passwords:

  • Ensure you use a minimum password length of 8 or more characters

  • Always include both upper, and lowercase, letters, numbers, and symbols

  • Avoid passwords you already use on other accounts

  • Avoid using information publicly associated with the user of the account such as date of birth, country, etc.

Prerequisites

To efficiently carry out this project, you need the following

  1. A code editor. (This project uses VScode)

  2. Python installed (Version 3 is installed for this project)

  3. Basic knowledge of Python programming language

  4. Understanding how a password generator works

Building the password generator

In this section, we will be exploring the two ways in which we can build a password generator.

The Easy Password Generator

Step one

The first thing you need to do in your code editor is to import random. This enables you to work with the random module. Next, you need to create a list of all the letters in the alphabet which includes lowercase letters and uppercase, and assign it to a variable. Do the same for all the numbers, and assign them to a variable. Lastly, create a list of all the symbols accepted in passwords and assign it to a variable. Your code should look like this

import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

Step Two

Print out a welcome note that displays when the user loads the software. Your welcome note can be as simple as “Welcome to Marvels Python Generator!”

Next, you want to ask the user to input the number of letters, symbols, and numbers they would love to have in their password. Remember to assign each of them to a variable and convert them all from string to integer. To do that, just add the int()as indicated below.

print("Welcome to the Marvels Password Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

Step Three

This is where the main work in the code begins so brace up!

Create an empty string and assign it to a variable called password.

It’s time to loop through all characters in your password. To do this, use the for loop and also use the range() to get hold of the number of letters the user wants in the password. For each position of the number chosen by the user, you will need to generate a random letter and add it to the password string created before. The code looks like this

password = ("")

for char in range(1,nr_letters + 1):
  password += random.choice(letters)

Now, you need to loop through all the symbols and numbers as we did above.

This is the final code of this step.

password = ("")

for char in range(1,nr_letters + 1):
  password += random.choice(letters)

for char in range(1, nr_symbols + 1):
  password += random.choice(symbols)

for char in range(1, nr_numbers + 1):
  password += random.choice(numbers)

Step Four

Now it’s time to print the easy password. To achieve this, use the print() and an f string to print the password

print(f"\nYour password is: {password}")

Final Code

building a password generator with python

Final Result

The Hard Password Generator

Repeat step one and step two.

Step Three

Create an empty list and assign it to a variable called password_list.

Next, add random letters based on the input number into the empty list. You can do this by using the for loop and also using the range() to get hold of the number of letters the user wants in the password. For each position of the number chosen by the user, you will need to generate a random letter and add it to the password string created before. Repeat the same step for symbols and numbers. The code will look like this:

password_list = []

for char in range(1, nr_letters + 1):
  password_list+= random.choice(letters)

for char in range(1, nr_symbols + 1):
  password_list += random.choice(symbols)

for char in range(1, nr_numbers + 1):
  password_list += random.choice(numbers)

Step Four

After creating a list with random letters, symbols, and numbers, you need to shuffle the list so that it does not follow the sequence but rather prints out the password in no predictable sequence.

To do this, we use the random.shuffle(). The code looks like this:

random.shuffle(password_list)

Step Five

Now, we convert the password from a list to a string. To do this, create a new variable called password and assign an empty string to it.

Using the for loop, loop through all the characters in the password_list and add each character to the empty password string.

password = ""
for char in password_list:
  password += char

Step Six

Print the password

print(f"\nYour password is: {password}")

Final Code

building a password generator with python

Final Result

Conclusion

A password generator saves you the stress of thinking of the right password to use whenever you are creating a new social account. Python programming language helps you get the job of building a password generator with few lines of code.

References

What is a password generator?

Guide on Password strength

Password Statistics