I started to learn Python, because I noticed it is used a lot by Cyber Security professionals, and was something that I wanted to be proficient at.

So, I decided to create a small program generating a random password using Python. First, let’s see the main.py file and analyze it.

πŸ’‘
main.py
import random
import uppercase
import lowercase
import number
import symbols
import randoms

print("Welcome to Password Generator")
print(" --- List of available options --- ")

print("Choice 1: Uppercase")
print("Choice 2: Lowercase")
print("Choice 3: Numbers")
print("Choice 4: Symbols")
print("Choice 5: Random")

print("Please select one choice: ")
choice = input()

if (choice == "1"):
	uppercase.uppercase_function()

elif (choice == "2"):
	lowercase.lowercase_function()

elif (choice == "3"):
	number.numbers_function()

elif (choice == "4"):
	symbols.symbols_function()

elif (choice == "5"):
	randoms.randoms_function()

else:
	print("Invalid Choice...")
	print("Ending program...")

First, we import the random library and all the following are individual python files that we included in our main.py using the import statement. We are waiting for input by the user and depending on the choice we call a function that lives inside all these individual .py files.

Now, let’s see the first .py file, in our case uppercase.py.

πŸ’‘
uppercase.py
import random

def uppercase_function():
    print("You selected Uppercase.")
    print("Give value 0 for exit...")
    letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    max_length = "20"

    print("Please enter the length of password: (max: 20) ")
    pass_length = input()

    if (pass_length == "0"):
        print("Exiting program...")

    def uppercase():
        count = 0
        while (count < int(pass_length)):
            random_letter = random.randint(0,25)
            print(letters[random_letter], end="")
            count = count + 1
        print()
        
    if pass_length <= max_length:
        uppercase()
    elif (pass_length == "0"):
        print("Exiting program..")
    else:
        print("Please enter a number less or equal to 20.")
        pass_length = input()
        if (pass_length == "0"):
            print("Exiting program..")
        while (pass_length > max_length):
            print("Please enter a number less or equal to 20.")
            pass_length = input()
            if (pass_length == "0"):
                print("Exiting program..")
        uppercase()

First, we define a string that contains all the letters in the English alphabet. We define a variable named max_length that contains the maximum length of the password. We wait for user input, regarding the length of the password. We define a function, uppercase(), that initiates a counter to the value of 0, and inside a while loop checks if that counter is less than the value that the user entered.

Afterward, we pick a random letter from the string that contained the alphabet, because in a sense a string is an array and increment the counter by 1, to avoid an infinite loop and print each letter in the same row.

In addition, we check if the length of the password is equal or greater compared to what the user entered and call the function uppercase() appropriately. Finally, we make a final check if the length of the password is 0, because if that’s the case the program should end because there is no password if there are no characters in it.

A word of notice here. The following Python files work in the same exact manner as the previous one, uppercase.py so if you understood that one, you will have no problem understanding the others.

πŸ’‘
lowercase.py
import random

def lowercase_function():
    print("You selected Lowercase...")
    print("Give value 0 for exit...")
    letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    max_length = "20"

    print("Please enter the length of password: (max: 20) ")
    pass_length = input()

    if (pass_length == "0"):
        print("Exiting program...")

    def lowercase():
        count = 0
        while (count < int(pass_length)):
            random_letter = random.randint(0,25)
            print(letters[random_letter].lower(), end="")
            count = count + 1
        print()

    if pass_length <= max_length:
        lowercase()
    elif (pass_length == "0"):
        print("Exiting program..")
    else:
        print("Please enter a number less or equal to 20.")
        pass_length = input()
        if (pass_length == "0"):
            print("Exiting program..")
        while (pass_length > max_length):
            print("Please enter a number less or equal to 20.")
            pass_length = input()
            if (pass_length == "0"):
                print("Exiting program..")
        lowercase()
πŸ’‘
number.py
import random

def numbers_function():
    print("You selected Numbers...")
    print("Give value 0 for exit...")
    numbers_list = "0123456789"
    max_length = "20"

    print("Please enter the length of password: (max: 20) ")
    pass_length = input()

    if (pass_length == "0"):
        print("Exiting program...")

    def numbers():
        count = 0
        while (count < int(pass_length)):
            random_number = random.randint(0,9)
            print(numbers_list[random_number], end="")
            count = count + 1
        print()

    if pass_length <= max_length:
        numbers()
    elif (pass_length == "0"):
        print("Exiting program...")
    else:
        print("Please enter a number less or equal to 20.")
        pass_length = input()
        if (pass_length == "0"):
            print("Exiting program...")
        while (pass_length > max_length):
            print("Please enter a number less or equal to 20.")
            pass_length = input()
            if (pass_length == "0"):
                print("Exiting program...")
        numbers()
πŸ’‘
symbols.py
import random

def symbols_function():

    print("You selected Symbols...")
    print("Give value 0 for exit...")
    symbols = "~`!@#$%^&*()_-+={[}]|\:;'<,>.?/"
    max_length = "20"

    print("Please enter the length of password: (max: 20) ")
    pass_length = input()

    if (pass_length == "0"):
        print("Exiting program...")

    def symbol():
        count = 0
        while (count < int(pass_length)):
            random_symbol = random.randint(0,30)
            print(symbols[random_symbol], end="")
            count = count + 1
        print()

    if pass_length <= max_length:
        symbol()
    elif (pass_length == "0"):
        print("Exiting program...")
    else:
        print("Please enter a number less or equal to 20.")
        pass_length = input()
        if (pass_length == "0"):
            print("Exiting program...")
        while (pass_length > max_length):
            print("Please enter a number less or equal to 20.")
            pass_length = input()
            if (pass_length == "0"):
                print("Exiting program...")
        symbol()
πŸ’‘
randoms.py
import random

def randoms_function():

    print("You selected Random...")
    print("Give value 0 for exit...")

    all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\:;'<,>.?/"
    max_length = "20"

    print("Please enter the length of password: (max: 20) ")
    pass_length = input()

    if (pass_length == "0"):
        print("Exiting program...")

    def alls():
        count = 0
        while (count < int(pass_length)):
            random_all = random.randint(0,92)
            print(all[random_all], end="")
            count = count + 1
        print()

    if pass_length <= max_length:
        alls()
    elif (pass_length == "0"):
        print("Exiting program...")
    else:
        print("Please enter a number less or equal to 20.")
        pass_length = input()
        if (pass_length == "0"):
            print("Exiting program...")
        while (pass_length > max_length):
            print("Please enter a number less or equal to 20.")
            pass_length = input()
            if (pass_length == "0"):
                print("Exiting program...") 
        alls()
πŸ’‘
Run Program
git clone https://github.com/mariosdaskalas/pgenie
cd pgenie/
python3 main.py

Or a one-liner command.

git clone https://github.com/mariosdaskalas/pgenie && cd pgenie/ && python3 main.py
πŸ’‘
Github

You can find the Source Code on Github.

Image by Jan Alexander from Pixabay

Tagged in: