Sitemap

Generating Passwords With Python

Create A Random Password Generator With Python

--

Press enter or click to view image in full size
Image of a laptop screen with VSCode window open
Photo by Chris Ried on Unsplash

With the present day GPUs and modern hardware, it has become increasingly easy for hackers to simply brute-force your password. Passwords that have a small range of characters and less overall length are more susceptible to such kind of attacks.

This has resulted in companies coming up with ridiculous password requirements, and fulfilling those requirements has become a challenge of its own.

The Solution

Instead of complaining, we can build ourselves a python bot to help us in this situation. Our goal is to make a bot that generates for us a password which fulfills all the site requirements.

Creating Lists of Valid Characters

Though different websites have different password requirements, there are some features that are common to all. We can safely assume that our website must contain at least one uppercase character, one lowercase character, a number and a special character.

Feel free in copy pasting all the code.

Choosing The Password Length

The next step is to decide the length for our password. Since most websites are opinionated about password length requirements, we can consider the password length as a variable and take it as an input from the user.

Get Akshat Mishra’s stories in your inbox

Join Medium for free to get updates from this writer.

But here’s the catch, since we have four types of characters which must be a part of our generated password. We must ensure that our password is not less than 4 characters in length, ensuring that all our characters occur at least once in our password

Password Length Function

To ensure that the user inputs a value not less than four, we have a while loop which runs infinitely as long as the value of password_length variable is less than four. If the inputted value is less than 4, we prompt the user to input a value which has a minimum length of four.

Creating A Function To Generate Password

To avoid any code clutter, we can divide each process into its own function. This is really helpful when it comes to code readability and debugging.

Cr

In the given function, we have referenced our character lists as global variables, this means our function can now access these global variables.

Generating The Password

The variable c, is a counter which iterates through each of the character lists to randomly select a character from a given sequence. Since we have four lists, for each of the four values of variable c (0, 1, 2 and 3), we pick a random character from each of the character lists.

When our variable c reaches 3, we reset the value of our variable to be -1, outside of the ‘if’ statement we add 1 to c, which makes it 0 again and the process continues until we have reached the length for our password.

Bringing It all Together

With all our components complete, we can finally stitch it all together in a single python script.

The Complete Script

The only module that this script requires, is the random module. It helps us in randomly choosing a character from any given Python sequence.

To take the input, we call the function getPasswordLength() and assign it to the variable length. We pass this length to our function CreatePassword(length), which gives us a password of our desired length.

Since our code is complete, we can execute it on the terminal or on the IDLE. To test our bot, I have chosen a password length of 8.

Press enter or click to view image in full size
Picture of The Python Terminal
Executing The Script

And this results in a password of our desired length. To secure our password even further, we can convert our password to a list and shuffle it. This will provide additional randomisation and make our passwords more secure.

Updated Script

The Convert(string) function takes a string as an input, and returns a list as an output. With our password randomized and shuffled, our bot is complete and secure.

Conclusion

With this we have solved a part of the problem, which was coming up with a password that fulfills the password requirements. But only a part of the problem, this creates another challenge which is remembering these crazy passwords for each of our different services. But that’s something we can save for another article.

--

--

Akshat Mishra
Akshat Mishra

Written by Akshat Mishra

Software Engineer, Writer & Marketer. Talks about Web3, Finance and Software Development