How does my computer generate passwords?
I was on the phone with a friend the other day, and she mentioned something that most of us have done:Using the same password for all her logins. Basically she ignores the passwords that the computer generates because they are easy to forget and it’s somewhat suspicious that a machine wants to give you a password.
So here is what i did. I wrote some code and sent it over to her and told her to use that to generate some new passwords. (I am that kind of a friend.. haha)
I am sure someone would like to know how it is done. So here, i will explain the code behind it in Python
step 1: Import the modules.
Basically the modules are files with python code. It’s like we are importing the code without having to write the whole code. The random module generates random variables. Give it an input, it will select randomly and give you an output.
For the string module, think as a way of importing letters of the alphabet, characters, numbers etc, without having to actually type them
step 2: Import the letters, numbers and characters you will use
letters = string.ascii_letters (uppercase and lowercase letters)
numbers = string.digits (numbers 0-9)
char = string.punctuation (the special characters e.g #@)
step 3: The user will input the number of letters, numbers and characters they want in their passwords.
no_of_letters = int(input('How many letters would you like in your password? '))no_numbers = int(input('How many numbers would you like in your password? '))no_char = int(input('How many characters would you like in your password? '))
Note: The word input will require the user to input the number of letters, numbers and special characters. This is not always the case. In fact, your computer wont ask you this step.
step 4: Depending on the number of letters, number and characters that the user inputs, generate an equal number of characters
password = ""
for letter in range (no_of_letters):
random_letter = random.choice(letters)
password += random_letter
for num in range(no_of_numbers):
random_number = random.choice(numbers)
password += random_number
for char in range(no_of_symbols):
random_symbol = random.choice(character)
password += random_symbol
- We start off with the password as an empty string.
- We will then incorporate the for loop to loop through the letters/numbers/characters in the range that the user inputs .
- I also executed a debugging code to ensure that the random_letter variable is a string type before concatenating(joining) to the original empty string.
step 5: Shuffle the string so that your password will not appear in any specific order.
password = random.sample(password, len(password))
final_password = ''.join(password)
#We need to shuffle the password in order that they don't appear in any specific order.
#The .shuffle() fuction doesn't work on strings. We will convert the password to a list, shuffle and then back to a string