Write a program to check password in Python
Data is an important asset in today's digital world. More than 8 million people fall victim to hackers every year and more than 2,200 cyber attacks happen every day. With an alarming rate of cybercrime, the use of methods to protect against hackers should be a top priority. One of the most important aspects of any online account is the password.
If a hacker steals your password, they can gain access to your personal information, even attack your bank account in just a few minutes. It's scary, but you can learn how to secure a password by building a Python program to test its strength.
Elements of a strong password:
- At least 12 characters long. The longer the password, the lower the risk of password theft.
- Password should include lowercase letters, uppercase letters, numbers and special characters.
- Avoid repeating characters in the password.
- It should not contain personal information such as names, phone numbers or any other data that can be easily guessed by others.
- The password should not be a common word, although you can use words in a longer passphrase.
- Do not set the same password for multiple accounts. You can use a password manager to automatically fill in passwords for different sites without having to remember them.
How to check password strength in Python
You can build a program to test password strength by checking the above characteristics. For this to work you need two modules: string and collections.
You can also use the getpass module to hide the password when the user enters it. This helps protect the password when you enter it publicly. To install getpass in a local environment, open a terminal and enter:
pip install getpass4
You can use the String class to check if a character is a letter, number, or symbol. You can use collection to check if there are repeated characters in the password.
Use the def keyword to define a function named check_password_strength() and pass the password you receive as a parameter to it.
Initialize 6 variables: lower_alpha_count, upper_alpha_count, number_count, special_char_count, length, common to 0 . They will check for the presence of lowercase letters, uppercase letters, numbers, special characters, length, repeating characters in the password.
Pass the password you have from the user to the list() function which converts it to a list of characters. Using loop, iterate through each sequence in the list. Use the if-else command to check if a character is a capital letter, upper case, number or special character and update the count accordingly.
Assume any letter of the alphabet or number is a special character. To increase the restriction, you can use a special character as a string and check to see if the character appears in the password. In addition, some websites allow you to use spaces in your password. You can set another variable and increment it, or consider using it in the special character counter itself.
import string import collections def check_password_strength(password): lower_alpha_count = upper_alpha_count = number_count = special_char_count = length = common = 0 for char in list(password): if char in string.ascii_lowercase: lower_alpha_count += 1 elif char in string.ascii_uppercase: upper_alpha_count += 1 elif char in string.digits: number_count += 1 else: special_char_count += 1
You will need a password list or database to check if the user's password is common or not. You can download a list of the million most common passwords from SecLists's Common-Credential GitHub repository.
Save the text file as common_password_list.txt . Use the with command to handle the exception and open the password file in read mode. Use the read() function to get the current content in the file and store it in a variable named content.
If the user-entered password does not appear in the list of common passwords, increment the value of the common variable by 1.
with open("common_password_list.txt", 'r') as file: content = file.read() if password not in content: common += 1
To check the length of the password, just pass it to the len() function and check if it is greater than or equal to 12. Increase the value of the length variable.
if len(password) >= 12: length += 1
To check the repeatability of characters in the password, use the Counter subclass from Collections. Counter is an unsorted collection of key and value pairs, where key is an element and value is the number of elements.
Use list comprehension to create a list of duplicate characters from the dictionary you have using collections. Iterate over the dictionary and check if the number of characters is greater than 1. If the number is greater, append it to a list and store it in the iterative list.
countOfWords = collections.Counter(password) repeated = [i for i in countOfWords if countOfWords[i] > 1]
Now that you have the values of each property, initialize a variable strength to 0. As the name suggests, you will see this variable tests the strength of the password. The scoring system is out of 7, one point for each inclusion of a character and the character of a strong password.
Checks the number of lowercase letters, uppercase letters, numbers and special characters greater than or equal to one and increases the strength of each existing character by 1. Similarly, check if the length and common variable are equal to one and the length of the repeated character is 0. If true, increase the value of the strength variable.
strength = 0 if lower_alpha_count >= 1: strength += 1 if upper_alpha_count >= 1: strength += 1 if number_count >= 1: strength += 1 if special_char_count >= 1: strength += 1 if length == 1: strength += 1 if common == 1: strength += 1 if len(repeated) == 0: strength += 1
Use a multiline string (3 quotes) to display multiple lines of text in a single print statement. Use String or f-strings interpolation or display the number of characters. You can achieve this by writing f at the beginning of the string in the print statement and enclosing the variables in curly braces. Here string interpolation is used because the content inside the print statement is a string, but the value of the variable is an integer.
print(f"""Mật khẩu của bạn là:- {lower_alpha_count} Chữ viết thường {upper_alpha_count} Chữ in hoa {number_count} Số {special_char_count} Ký tự đặc biệt {length} Độ dài {common} Phổ biến {repeated} Lặp lại "Password score: {strength}/7""")
Finally, use the input command to get the password from the user and pass it to the check_password_strength function . Based on the strength of the password, this program will show the score and the number of characters of the password.
password = input("Enter the password: ") check_password_strength(password)
Password strength test results
When entering a strong password based on the mentioned characteristics, the Python program shows a strength of 7/7:
When entering weak and common passwords, the Python program shows strength as 1/7:
Above is how to write a program to check passwords in Python . Hope the article is useful to you.
You should read it
- Write a program to check duplicate values in Python
- Python online editor
- How to set up Python to program on WSL
- Write an alarm clock program in Python
- Write a program to find the majority element in an array in Python
- More than 100 Python exercises have solutions (sample code)
- Write a program to print Pascal's Triangle in Python
- Package in Python
- Bookmark 5 best Python programming learning websites
- Write a program to calculate the square root of a number in Python
- Write a program to find duplicate values in Python
- For in Python loop
Maybe you are interested
How to retrieve Facebook password, recover latest account
How to Easily Move All Passwords from iPhone to Android
The simplest way to get back your Roblox account when you forget your password
How to log in to Wifi Modem Router to change Wifi name and password
How to set password for application on Windows 11 computer
How to try to crack a password yourself to test its strength