Creating a Random Password Generator with Python

We use passwords just about every day for various aspects of our lives. They allow us to authenticate who we are and provide access to our sensitive data in the multitude of places we store it. What many people don’t realize though is how easy it is to crack short, simple passwords. With processing power continually increasing, the amount of time it takes to brute force a password continues to decline. Trying to be clever and increasing the complexity of ‘password’ to ‘P@$sw0rD’ has virtually no effect on the amount of time it would take to crack it. The best solution is to use long, strong, and random passwords.

A recent study shows how quickly someone can brute force a password at varying lengths and complexity levels. And this is with readily available off the shelf hardware.

https://www.hivesystems.io/password

Based on this chart you will want to consider at least a minimum of 12 characters including numbers, upper and lowercase letters, as well as symbols. But how inconvenient is that? Who can come up with random passwords so readily? Humans typically always work in patterns, so even when you think you can come up with something random it likely won’t really be. Thankfully we have the convenience of programming to help us out. With the programming language Python we can write our own random password generating script with just barely more than a handful of lines of code.

#!/usr/bin/python3
# Random password generator for strong, secure passwords!

import random
import string
import sys
pass_length = sys.argv[1]

def generate_random_string(pass_length):
    letters = string.ascii_letters + string.digits + string.punctuation
    random_string = ''.join(random.choice(letters) for i in range(int(pass_length)))
    return random_string

random_string = generate_random_string(pass_length)
print(random_string)

This simple script will provide us with a randomly generated password with a length of our choosing, containing numbers, upper and lowercase letters, and symbols.

#!/usr/bin/python3
# Random password generator for strong, secure passwords!

This first line is the shebang, which tells this Linux system which interpreter to use for running the script. The second line just explains the purpose of the script.

import random

This line imports the Random module which is a built-in Python module. It will allow us generate random integers using Python.

import string

This line imports the String module which is a built-in Python module. This will allow us to get all of the different type of characters we will need.

import sys

This line imports the Sys module which is a built-in Python module. This will allow us to take arguments from the user and pass them into the script.

pass_length = sys.argv[1]

This line sets the variable ‘pass_length’ equal to the second argument provided by the user. When running the script from the command line you will essentially provide two arguments, the script itself, and the second which will be a number denoting the desired length of the password.

def generate_random_string(pass_length):

This line defines the function ‘generate_random_string’ and passes into it the argument ‘pass_length’ which was defined by the user from the command line.

    letters = string.ascii_letters + string.digits + string.punctuation

This line sets the variable ‘letters’ equal to all available letters, digits, and punctuation symbols available with the ASCII encoding format. ASCII essentially contains all the characters we most often work with even though ASCII has some limitations and has been mostly replaced with Unicode. For our purposes though it will provide us with exactly what we need.

    random_string = ''.join(random.choice(letters) for i in range(int(pass_length)))

This line has a lot going on, we’ll start from the left. Firstly we’re setting the variable ‘random_string’ to what will be the eventual password. To do this we’re taking the variable ‘letters’ which contains all of the ASCII characters and randomly selecting one of those characters with the ‘random.choice’ function. We will use the ‘join’ method to add this character to the variable ‘random_string’ and we use the separator ‘’ so that there will not be any white space between characters. The ‘for’ loop will loop through this random character selection the amount of times set by the ‘pass_length’ argument. Be sure to use the ‘int’ function so the ‘pass_length’ argument won’t be interpreted as a string.

    return random_string

This line uses the ‘return’ statement which will exit the function and return a value. The value in this case is the variable ‘random_string’ which was derived within the function.

random_string = generate_random_string(pass_length)

This line will actually run the function that we defined. We want to run the ‘generate_random_string’ function with the argument ‘pass_length’ and set it to the variable random_string.

print(random_string)

Lastly, we have to print that ‘random_string’ variable to see what the new random password will be. We can copy and print the result as necessary.

Now we can run the script from the command line, making sure to include a number to denote the length of the password we want to randomly generate. See some examples in the image above.