Skip to main content

Make Password Generator using Python

In our previous Python tutorial, you have learned how to Monitoring Memory Consumption in Python. In this tutorial, you will learn how does a password get hacked and how to Generate a Strong Password in Python.

Having a secured password is very important because there are always security threats such as cyberattacks, password leaks, and data breaches. So be always cautious to use strong and secure password to avoid the chances of Brute force attack or any other security breach.

You can easily create strong and secure password with Python using Random or Secrets module. But here we will use Secrets modules which helps to create much secure password than Random module.

How does a password get hacked?

Brute force attack is a common way to hack password. The attacker’s automates software with many combination in a quick time to hit your password. If password length is small then there will be very less combination needed to break in a short time. The password length is important, generally password less than 12 characters is vulnerable to being cracked. So password should be long enoguh. The longer, the stronger.

Tricks to create password

To avoid brute force attacker’s, you can following few tricks while creating password:

  • Make password long: This is the most critical factor. Choose nothing shorter than 12 characters, more if possible.
  • Always use mix of characters: Always try to mix up letters (upper-case and lower-case), numbers, and symbols, the more potent your password is, and the harder it is for a brute force attack to crack it.
  • Avoid common substitutions: The password crackers are smart enough with substitutions. Whether you use DOORBELL or D00R8377, the brute force attacker will crack it with equal ease.
  • Always use numbers or special characters to finish words: You can use numbers and special characters to finish certain words of the password to make it more secure.

Create Secure password with Python

Now we will create secure password using String and Secrets modules from Python.

String Module

The String module is a built-in module and we don’t need to install it. It is used to manipulate strings. This module comes with many useful function to work with strings. We will use function from this module to get string to generate strong password.

Secrets Module

The Secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. We need to install Secrets module using below command.

pip install secrets

Let’s generate password

We will create Python file and import required modules.

import secrets
import string

After importing the modules, we will define the length of our password. We will set it 20 character long to make it more secure.

passLength = 20

Then we will get the string for password using String module function. We will get all lower and upper case ASCII characters, all the integer values from 1-9, all the special characters.

lowerLetter = string.ascii_lowercase
upperLetter = string.ascii_uppercase
numbers = string.digits
symbols = string.punctuation

After that we concatinated the strings into a single string.

allChar = lowerLetter + upperLetter + numbers + symbols

Finally, we will generate password by using secrets.choice() function and providing single string and password length.

password = ''.join(secrets.choice(allChar) for i in range(passLength)) 

Complete code to generate password:

import secrets
import string

passLength = 20

lowerLetter = string.ascii_lowercase
upperLetter = string.ascii_uppercase
numbers = string.digits
symbols = string.punctuation

allChar = lowerLetter + upperLetter + numbers + symbols

password = ''.join(secrets.choice(allChar) for i in range(passLength))  

print("\n\nYour password is: "+password+"\n\n")

Output:
When we run above code, it will generate random password like below.

Your password is: ?N3XU-<s9~lfDvCoj}%!

In this tutorial you have learn how passwords are hacked and know the tricks to create secure password. You have also learn how easily you can create strong and secured password using Python. Hope you have enjoyed this tutorial, please let me know in the comments if you face any concern or question.