"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Building a Password Strength Checker in Python

Building a Password Strength Checker in Python

Published on 2024-11-01
Browse:484

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 



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?

  1. It starts with a score of 0.
  2. It adds 1 point for each length threshold passed.
  3. It adds 1 point for each type of character used (uppercase, lowercase, special, digit), minus 1.
  4. 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 



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

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

Building a Password Strength Checker in Python

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!

Release Statement This article is reproduced at: https://dev.to/immah/building-a-password-strength-checker-in-python-47om?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3