This content originally appeared on DEV Community and was authored by Vaarun Sinha
In this tutorial we will make a password generator with the secrets module. Why not random module? see the first post in this series to understand.
So Let's Begin!
Problem
- Generate a random password, according to the user input.
Sub problems
- Get user input
- Create lists for letters etc...
- Loop over lists and make a password.
- Print the password.
Let's write some code!
- Get user input Here we will use try/except blocks so that when user types a string we do not get an error.
We will get inputs for number of special characters, small letters, big letters and numbers required for generating the password.
Code:
try:
num_of_numbers = int(input("How many numbers should your password have: "))
except ValueError:
num_of_numbers = int(input("How many numbers should your password have: "))
try:
num_of_special = int(
input("How many special characters should your password have: "))
except ValueError:
num_of_special = int(
input("How many special characters should your password have: "))
try:
num_of_small_letters = int(
input("How many small letters should your password have: "))
except ValueError:
num_of_small_letters = int(
input("How many small letters should your password have: "))
try:
num_of_cap_letters = int(
input("How many big letters should your password have: "))
except ValueError:
num_of_cap_letters = int(
input("How many big letters should your password have: "))
### Create Lists for numbers to choose from.
- We could hardcode this like:
number_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
But this will take us way more time!
So let's use the string module!
import string
number_list = list(string.digits)
But we could just have a string right? then why make it list you will find this out later on in this blog!
Let's do this for everything.
import string
smallLetters = list(string.ascii_lowercase)
bigLetters = list(string.ascii_uppercase)
specialCharacters = list(string.punctuation)
numbers = list(string.digits)
So now we can loop over these lists, and make a list!
First let's declare empty variables!
spPart = ""
numPart = ""
smallPart = ""
bigPart = ""
Now let's loop over the lists.
for i in range(1, num_of_numbers + 1):
randNum = secrets.choice(numbers)
numPart = numPart + str(randNum)
for i in range(1, num_of_special + 1):
randSp = secrets.choice(specialCharacters)
spPart = spPart + randSp
for i in range(1, num_of_small_letters + 1):
randSm = secrets.choice(smallLetters)
smallPart = smallPart + randSm
for i in range(1, num_of_cap_letters + 1):
randBig = secrets.choice(bigLetters)
bigPart = bigPart + randBig
Now let's add the parts to make a password.
password = numPart + spPart + smallPart + bigPart
Now let's refactor the code, by putting this into a function.
def generate_password( num_of_numbers, num_of_special, num_of_small_letters, num_of_cap_letters):
smallLetters = list(string.ascii_lowercase)
bigLetters = list(string.ascii_uppercase)
specialCharacters = list(string.punctuation)
numbers = list(string.digits)
spPart = ""
numPart = ""
smallPart = ""
bigPart = ""
# ANCHOR: Generating Password
for i in range(1, num_of_numbers + 1):
randNum = secrets.choice(numbers)
numPart = numPart + str(randNum)
for i in range(1, num_of_special + 1):
randSp = secrets.choice(specialCharacters)
spPart = spPart + randSp
for i in range(1, num_of_small_letters + 1):
randSm = secrets.choice(smallLetters)
smallPart = smallPart + randSm
for i in range(1, num_of_cap_letters + 1):
randBig = secrets.choice(bigLetters)
bigPart = bigPart + randBig
password = numPart + spPart + smallPart + bigPart
return password
Now if we print/generate the password every time ,there is a predictable format, first numbers then special etc..
So how we can solve this?
We will solve this in the next short blog coming very quickly after this blog, so stay tuned!
Happy Coding!
This content originally appeared on DEV Community and was authored by Vaarun Sinha
Vaarun Sinha | Sciencx (2021-11-18T09:32:09+00:00) How to make a password generator with python.. Retrieved from https://www.scien.cx/2021/11/18/how-to-make-a-password-generator-with-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.