Simple Password Generator
As a part of my weekly coding challenges I decided to create simple password generating program that allows an user to create strong and unique passwords depending on the wanted legth. It's great way to improve your programming skills and problem solving. Let's see how it works!
I will break down the code of the program into smaller pieces that will let you step by step understand how it works. We're going to use 2 built in libraries - random to create as random passwords as it's possible and string to let us work on the characters used in password.
Firstly, the main function should ask the user for the length of the password he wants to create - no one will use a program that will only generate passwords made of 10 characters. But we should also remember that the user input should be checked whether or not it's a digit since we don't want the password of length "t".
But what next? As our program generates only strong passwords made of lowercase and uppercase letters, symbols and numbers and then checks for at least one of each we should consider the minimum length of the password. Any password shorter or equal to 3 character will never be generated, and we don't want to make the user sit in front of the screen for hours and hours just to generate short password.
Okay, so now we have the filter that won't allow password shorter or equal to 3 characters but what happens after it? Firstly we define that password_strong is false as we haven't checked it yet. But what happens next?
- While not False: results in while True:
- Then the loop proceeds to execute the following block of code: generating the password and checking for it's complexity
- If the check ends in False then the loop goes on, meaning that the password has been generated but it was missing essential elements to pass the check.
- If the check ends in True then the loop ends, meaning that the password has been generated and it's the strong one.
After generating the strong password, program prints it on the screen.
Now let's explore the most essential function in this program which generates a password.
Let's see how it works step by step. Firstly we have an empty string in which we're going to add our password. After that we're going to use random choice which basically will create a random character from the predefined set of characters (like string.ascii_letters which are all letters uppercase and lowercase) "length" number of times. So if we have length 4 it will four times get the random character from the random set and then it will add it to the string. Use of random.SystemRandom will make sure that these are cryptographically secure PRNGs. Easy, right?
This function is pretty self explanatory, it checks whether or not password is made from at least one uppercase letter, one lowercase letter, one digit and one special character. It returns True if the password is secure and False if it's not.
That's why I love python, even without knowing programming any person could interpret the code. It sums the number of each parameter by checking if it's in the certain set, letter by letter. If it occurs, it will add to the sum 1, if not the sum will stay the same. Then it checks if the password contains at least one of each character type, resulting in True if the password is strong and False if it's not strong one.
At the end I added the:
By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves. Check out whole code at my Github account.
I hope you enjoyed exploring the concept behind this small program. Remember that doing a lot of small challenges like that one will help you a lot in learning programming. Good luck with creating your own versions!