Fun with Python: A Simple Password Generator


Introduction

In this installment of FwP, I have created a new script, just for fun, that can generate random passwords up to 124 characters (I just wanted to add a limiter).

As always, this script is meant for educational purposes, and is free to use under the GNU GPL with no guarantees, or warranties. The user assumes all risk and responsibility for its use, and misuse.

The code was written and tested in Windows. I assume some basic level of computer skills in my instructions.


Preparation

Unlike previous entries, this script is a standalone application, so no prep work is required.


The Code

Open a new Notepad window, then copy and paste the following code. Name it “passgen.py” and save it anywhere. Make sure to select All Files under the option for Save as type so that it is saved as a .py file.

 

import os import random app_running = True def menu(): ui = "" pw_type = 1 pw_lenght = 8 print("*********** simple password generator ***********") print("(1) Generate a Single Password ") print("(2) Generate a List of Passwords ") print("(q) Quit the Program ") print("***************************************************") while app_running: print("Make a selection from the menu.") ui = input("_> ") match ui: case "q": app_running == False quit() case "1": get_length(1) case "2": get_length(2) case _: print("Error: command not recognized.\n") def get_length(pw_type): no_length = True while no_length: print("\nInput the password length (min: 8 | max: 124).") pwl = input("_> ") if pwl.isdigit(): i_pwl = int(pwl) if 13 < i_pwl < 125: no_length = False generate(pw_type,i_pwl) if 7 < i_pwl < 14: print("\nWARNING: A password of",i_pwl,"is only acceptable if you also implment MFA, though longer is always better.") no_length = False generate(pw_type,i_pwl) if i_pwl < 8: print("\nA password of that length is not recommended. Create a longer one.") else: print("\nError2: Invalid entry.") def generate(pw_type,i_pwl): pw = [] password = "" pw_chars = ["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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "#", "$", "%", "^", "@", "!", "&", "*", "(", ")", "_", "-", "+", "=", "[", "]", "{", "}", "|", ":", ";", ",", "\"", "'", "<", ">", ".", "/", "?"] match pw_type: case 1: for _ in range(i_pwl): pw_char = random.choice(pw_chars) pw.append(pw_char) password = "".join(pw) print("\nHere is a password with",i_pwl,"characters:\n", password, "\n") case 2: print("\nHere is a list of 10 passwords with",i_pwl,"characters:\n") for _ in range(10): pw = [] for _ in range(i_pwl): pw_char = random.choice(pw_chars) pw.append(pw_char) password = "".join(pw) print(password) case _: pass print("\n") menu() def password_list(): pass os.system('cls' if os.name == 'nt' else 'clear') menu()


The Script in Action

Open a Command Prompt and navigate to the script’s location.

Type the following command:

python passgen.py

This will run the script, which first clears the screen, then presents a menu and an input prompt.

Type 1 and then press Enter to select the option Generate a Single Password. It will then prompt for the password length.

Type any number from 8 to 124. It will accept numbers less than 8, but will error out and suggest creating a longer password. Choosing any number in the range 8 to 13 inclusively, will result in a message that states MFA should be enforced if a password of that length is used. These constraints were added to align with Safeguard 5.1 of the CIS Critical Security Controls v8.1, and are considered Essential Cyber Hygiene.

After choosing a length, press Enter. it will then output a random password of the chosen length, and return to the main menu.

Select the second option Generate a List of Passwords. Type 2 and press Enter. Again, it will prompt for a password length. Type an appropriate length, and press Enter. It will then generate a list of 10 random passwords with the length provided.


Conclusion

I will not dissect the code this time. It is not overly complicated and it could probably use some tweaking. It was fun to create and test. I had some difficulty at first with generation portion due to incorrectly nested for loops, and forgetting to clear the list with each iteration, but I got there in the end.

I hope you find some use of it. If not, at least I learned something and now have a handy tool for myself.

Again, this script is meant for educational purposes, and is free to use under the GNU GPL with no guarantees, or warranties. The user assumes all risk and responsibility for its use, and misuse.


Daily Cuppa

Today’s cup of tea is again Tulsi Masala Chai provided by Organic India. Organic, and fair trade. As this will go very well with the Indian cuisine I have for dinner, I will brew it in the same fashion as yesterday:

Steep the tea a little long to get as much flavor as possible from the leaves, then add a few dashes of cinnamon, cardamom, turmeric and ginger, and finally mix it with oat milk, for a homemade chai latte. A nice accompaniment to the aromatic and flavorful meal.


If you enjoyed this article or the site in general, feel free to  buy the author a cup of tea.

Previous
Previous

Tea with Copi: Music to Hack to

Next
Next

Threat Intel Collaboration Resources for the Embattled Cyber Defender