Hello again, and welcome to today’s tutorial. Today, we are going to build a simple password strength checker using Python. We’ll explain how the code works step-by-step and give tips on how to improve your passwords.
Why is Password Strength Important?
A weak password makes it easier for hackers to guess or crack using various methods, putting your personal information at risk. A strong password is:
- Long enough (at least 12 characters)
- Uses a mix of letters (both uppercase and lowercase), numbers, and special characters
- Avoids common or predictable words
Let’s get started by building a tool that assesses the strength of a password based on these rules.
Setting Up Your Environment
Before we start coding, make sure you have Python installed on your computer.
Create a new Python file where you will write your code. Also, download this file that contains the most common passwords (we will look more into that later) and save the file in the same directory as your Python file for this project.
Import Necessary Libraries
import string
The string
module provides useful constants for checking character types like uppercase letters, digits and special characters.
Check for Common Passwords
def check_common_password(password):
with open('common-password.txt', 'r') as f:
common = f.read().splitlines()
if password in common:
return True
return False
This function checks if the given password is in a list of common passwords.
- It opens a file called ‘common-password.txt’ which we downloaded earlier.
- It reads all the passwords from this file into a list.
- If the input password is in this list, it returns
True
(meaning it’s a common, and therefore weak, password). - Otherwise, it returns
False
.
Why is this important? Many hackers start by trying common passwords, so using one makes your account very vulnerable.
Assess Paswword Strength
def password_strength(password):
score = 0
length = len(password)
upper_case = any(c.isupper() for c in password)
lower_case = any(c.islower() for c in password)
special = any(c in string.punctuation for c in password)
digits = any(c.isdigit() for c in password)
characters = [upper_case, lower_case, special, digits]
if length > 8:
score += 1
if length > 12:
score += 1
if length > 17:
score += 1
if length > 20:
score += 1
score += sum(characters) - 1
if score < 4:
return "Weak", score
elif score == 4:
return "Okay", score
elif 4 < score < 6:
return "Good", score
else:
return "Strong", score
This function evaluates the strength of the password based on several criteria.
- The password gets points for being longer than 8, 12, 17, and 20 characters.
- It checks for the presence of uppercase letters, lowercase letters, special characters, and digits.
How does the scoring works?
- It starts with a score of 0.
- It adds 1 point for each length threshold passed.
- It adds 1 point for each type of character used (uppercase, lowercase, special, digit), minus 1.
- Based on the final score, it categorizes the password as “Weak”, “Okay”, “Good”, or “Strong”.
Provide feedback to the User
def feedback(password):
if check_common_password(password):
return "Password was found in a common list. Score: 0/7"
strength, score = password_strength(password)
feedback = f"Password strength: {strength} (Score: {score}/7)n"
if score < 4:
feedback += "Suggestions to improve your password:n"
if len(password) <= 8:
feedback += "- Make your password longer (more than 8 characters). n"
if not any(c.isupper() for c in password):
feedback += "- Include uppercase letters.n"
if not any(c.islower() for c in password):
feedback += "- Include lowercase letters.n"
if not any(c in string.punctuation for c in password):
feedback += "- Add special characters (e.g., @, #, $).n"
if not any(c.isdigit() for c in password):
feedback += "- Add numbers.n"
return feedback
This function combines the previous two functions to provide comprehensive feedback.
- It first checks if the password is common. If so, it immediately returns a warning.
- If the password is not common, it assesses its strength.
- It provides the strength rating and score.
- If the password is weak (score < 4), it gives specific suggestions for improvement.
Main Program
password = input("Enter the password: ")
print(feedback(password))
This last part simply asks the user to input a password and then prints the feedback.
Program in Action
Conclusion
Creating strong passwords is a vital part of maintaining online security, and with this tool, you can easily assess how secure your passwords are. This simple program demonstrates how basic Python programming can be combined with cybersecurity principles to solve a real-world problem.
Feel free to experiment with the code and add more features.
Happy coding, and stay secure!
Source link
lol