Activity 3
Learning how to create a password generator can be the perfect project to learn or review basic Python and Kivy concepts.
To create a password generator in Python you can use a for loop that randomly selects alphanumeric characters, digits and punctuation characters to generate the password string. You can set the password length that defines the number of loop iterations. Also, using a nested for loop you can improve the password generator to generate multiple passwords.
We will start with a simple password generator and then we will refactor the code to make it more flexible.
Using your skills and knowledge from Activity 1 start a new Kivy project to create your password generator. The following tutorial will focus in on the code that will be used for your button. You will need to setup the GUI.
We will start by generating a random string of 12 characters. To do that we will use the function random.choice() that returns a random character from a sequence.
Import the random module. Set the password length to 12. Define a list of characters that we will use to generate the random password. In this first version of the program we will use just a few letters and numbers. Create an empty string called password. Write a for loop that executes 12 iterations and that at every iteration selects a random character from the string characters and appends it to the password string. Print the password we have generated. Import Random at the top of your code
|
|
The following will need to be triggered by a button.
|
|
And here is an example of a password generated with this code:
Password generated: dcb4a2c4aac5
In the next section we will make it more secure.
Let’s improve the complexity of the password by using all alphanumeric characters.
To do that we could simply update the value of the characters string to include all letters and numbers but it would be time consuming and prone to errors.
What can we do instead?
We can import the Python string module that provides a few constants that we can use in the generation of a password.
Here are few examples:
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
We won’t use string.ascii_lowercase or string.ascii_uppercase considering that they are both included instring.ascii_letters.
In our password generator we will use the following three sets of characters:
string.ascii_letters string.digits string.punctuation We will use the + symbol to concatenate the three sets of characters to create a single string that we will assign to the characters variable.
This is what you get if you concatenate the three sets of characters in the Python shell:
>>> string.ascii_letters + string.digits + string.punctuation
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
|
|
Here are three examples of passwords generated with the updated program:
Password generated: iE%g.JqurkB0
Password generated: |>J+qbZ<Vl7$
Password generated: c94,JRgshz#g
Update the Password Generator to Receive the Password Length as User Input Let’s make our password generator a bit more flexible.
We will allow the user to provide the password length instead of hardcoding it in our program. To do that we will use the input function.
|
|
Notice that we have converted the value returned by the input function into integer considering that when using Python 3 the input function returns a string.
The program works fine.
Confirm it works on your machine too before continuing with this tutorial.
Provide the password length: 12
Password generated: ]"c_ga%M^iOd
If you don’t convert the output of the input function into integer you get an error when you use the variable password_length in the for loop.
How to Create a Python Password Generator that Generates Multiple Passwords In this section we will enhance our password generator to generate a custom number of passwords.
The approach will be very similar to the one we have used to get the password length from the user with the input function.
We will do the following:
Get the number_of passwords using the input function. Add a for loop to generate multiple passwords based on the value of the variable number_of passwords provided by the user.
|
|
Let’s test our code:
How many passwords do you want to generate? 3
Provide the password length: 8
Password 0 generated: 2B.1&=~k
Password 1 generated: Wt$@1vi'
Password 2 generated: ,aOXN_@$
How to Generate Strong Passwords in Python Before completing this tutorial let’s find out how we can enforce a set of rules to make sure the passwords we generate are strong enough.
We will make sure passwords contain at least:
- three digits
- two punctuation characters
To do that we will use two integers that define the number of digits and punctuation characters. Then we will use multiple nested for loops.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import random, string number_of_digits = 3 number_of_punctuation_characters = 2 characters = string.ascii_letters + string.digits + string.punctuation number_of_passwords = int(input("How many passwords do you want to generate? ")) password_length = int(input("Provide the password length: ")) for password_index in range(number_of_passwords): password = "" for digits_index in range(number_of_digits): password = password + random.choice(string.digits) for punctuation_index in range(number_of_punctuation_characters): password = password + random.choice(string.punctuation) for index in range(password_length - number_of_digits - number_of_punctuation_characters): password = password + random.choice(string.ascii_letters) print("Password {} generated: {}".format(password_index, password))
Let’s have a look at a few password generated with this program:
How many passwords do you want to generate? 3
Provide the password length: 10
Password 0 generated: 738<>ZKwMA
Password 1 generated: 332|(SlZDT
Password 2 generated: 756%#NFWHs
To shuffle characters in the password string we will use the random.shuffle() function.
We will create a specific function that does the shuffling.
|
|
Then update the last print statement to call therandomize_password function.
|
|
How many passwords do you want to generate? 3
Provide the password length: 10
Password 0 generated: X1+dT)4U1q
Password 1 generated: 2T7g+OQ-B4
Password 2 generated: g3n0<3O>if
In this tutorial you have learned how to create a password generator in Python and how to update its logic to increase the strength of the passwords.
We have covered a few Python core concepts:
- importing modules.
- defining functions.
- using the input function to read user input.
- nested for loops.
- selecting random characters from a string and shuffling a string.
- using the string format method.
- Watch the following video Change Background Color And Text Color of Labels to improve the look of your app.
- Watch the following video How To Use Images With Kivy and add an image to your app.