Python Password Generator Tutorial – Five Minute Python Scripts

Python Password Generator Tutorial – Five Minute Python Scripts

Python Password Generator Tutorial - Title Slide
~5 Minute Read

Making a Random Password Generator with Python

Requirements:

  • Python 2 / Python 3
  • Python string module
  • Python Random module

In this post we’ll go over how we can create a python script that will generate a random password. This post is mainly designed to go more in depth than the youtube video I’ve already made on this topic. You can watch the video below for the baseline. 

The Code used in this video was:

import string
from random import *
characters = string.ascii_letters + string.digits + string.punctuation
password = “”.join(choice(characters) for x in range(randint(8,16)))
print(password)
Line by line, we’re importing the string and random module, assigning the characters we want our password to be made out of, and then create the password from a random number of those characters (8 to 16) in the above code. 

Expanding This Code

Same Word, Random Numbers After

But how can we take this code further? Suppose you have a password that you use for almost every website (You shouldn’t!! But it’s common.) And instead of a random password, you want to expand off this password with a given set of numbers.

We can do this super easily with code that looks like the below:

import string
from random import *
characters =  string.digits 
password = “YourWord“.join(choice(characters) for x in range(randint(4,6)))
print(password)
This script would allow you to create a string using your base password, then randomly generate four to six numbers after the password. 
 
If you’re concerned about password security though, realize that if your word ever gets discovered – which it probably will if it is something important to you such as a name, or place – that it would only take 10^4 to 10^6 brute force attempts until someone cracked your password. 
Recreating Your Randomly Generated Passwords
The tough part of using randomly generated passwords is the whole remembering them part. But what if you could recreate the random password in your Python script so you could call them up later? We can do this with a random seed value.
Let’s look at the code below:

import string
from random import *
seed(9)
characters = string.ascii_letters + string.digits + string.punctuation

password = “”.join(choice(characters) for x in range(randint(8,16)))

print(password)

This script will create the same random password every time. Let’s cover a slight side note on why this happens.

Python’s random module is a psuedo random module. That is, it performs some operation that isn’t disclosed to pick the next random values. It works from memory, so after every random password, it’ll take the previous result and do that operation on those results.

The downside? There isn’t a value in memory from the beginning. So we can seed, or set that beginning value, to reproduce the results.

In this scenario, we could write our script, remember the numbers of our seed, and be able to reproduce the passwords. 

 

Creating Truly Random Passwords

So now, we see that passwords here are not truly random, but rather a product of the function of the python randomizer. So how can we make these truly random? We can seed the operation with the current date time, which is always different and therefore will always produce a different result. 

import string
from random import *
import datetime

now = datetime.datetime.now()
seed(now)
characters = string.ascii_letters + string.digits + string.punctuation

password = “”.join(choice(characters) for x in range(randint(8,16)))

print(password)

Every time you execute this script, the password generator will produce something entirely new and not able to be replicated in the script. 

Enjoy this content? Subscribe to the newsletter!

Leave a Reply

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