Generate Passwords using Python and Secrets

Almost every day I get a notification from different websites about cyberattacks, password leaks, and data breaches. Having a secured password is very important as it reduces the chances of Brute Force attacks where the hacker will keep on guessing the passwords until it hits the right one. You can Generate Passwords using Python and a module called Secrets which is much secured than the Random module.

Generating a secured password by yourself is a kind of difficult task. You’ll always try to use a password that you can remember easily. For example, some of the most common passwords are Qwerty@123 or 123456. These passwords can be very easily guessed by anyone, even by a kid!

In this article, we’re going to create a Python program which will generate a secured password which you can use anywhere.

What are we going to cover?

  • Discuss different Python packages which will be used to generate a secured password.
  • Generate Passwords using Python and Secrets module

Python Modules

String Module

This module in Python is used to manipulate strings. It comes with a lot of functionalities that we can use in our Python code for strings.

string.ascii_letters(): This returns all the ASCII letters which are available.

string.ascii_uppercase(): This return all the ASCII letters in upper case.

string.ascii_lowercase(): This return all the ASCII letters in lower case.

string.octdigits(): This return all the octal digits.

string.hexdigits(): This return all the hexa digits.

string.digits(): This return all the digits.

string.punctuation(): This return all the special characters or punctuations.

string.capwords(): This makes the first character of each word in the capital title. It also takes a second parameter which can be used as a separator for capital letters.

So these are the functions provided by the String module in Python which we’ll use to make the password stronger.

Secrets Module

Secrets module is used for generating cryptographically strong random numbers which are best for managing data like passwords, tokens, etc. Why don’t we use the Random module here? It is because the numbers generated by the random module can be determined by an initial value called PRNG’s seed. Which means there’s no true randomness in the Random module.

Let us write some code

Now open your favourite IDE, open a new file “password_generator.py” and paste the below code:

# import the necessary modules!
import secrets
import string

print('Welcome to Password generator by GeekyHumans!')

#input the length of password
length = int(input('\nEnter the length of password: '))                      

#define data
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation
# combine the data
all = lower + upper + num + symbols

#use secrets

password = ''.join(secrets.choice(all) for i in range(length))  

#print the password
print(password)

Code Explanation

In the above code we have imported two modules: String and Secrets which we’ll use to generate our strong password. After importing the modules we’re asking for the length of the password so that our password generator can generate the password of the defined length.

lower = string.ascii_lowercase
 upper = string.ascii_uppercase
 num = string.digits
 symbols = string.punctuation

We then have defined four variables:

  • lower: Which contains all the lower case ASCII characters
  • upper: Which contains all the lower case ASCII characters
  • symbols: Which contains all the special characters
  • num: Which contains all the integer values from 1-9

After that, we concatinated the strings to form a single string.

all = lower + upper + num + symbols

At last, we creating the password of the given length by joining the string generated by secrets.choice()

password = ''.join(secrets.choice(all) for i in range(length))  
#print the password
print(password)

Output:

Welcome to Password generator by GeekyHumans!
Enter the length of password: 100
 sY|Wr%J8g%f1r=B<I0GP;"o4C7Pkr^NE;y^d@Wz}A{^?l39e^}xtXFhY4=pTce-luz?O!Pvyj1"75?P%VOH%d?=:-!]MJRgZ%$vy

Final Words

See how easy it was to generate your own secured password? I hope you like this tutorial and you were able to create your own password generator. The concept is very easy and just uses some basic functions. Please let me know in the comments if you face any problems. You can also checkout how I automated tweets of my blog posts.

The post Generate Passwords using Python and Secrets appeared first on Geeky Humans.