Password Checker in Python

Photo by RetroSupply on Unsplash

Password Checker in Python

Securing Your Digital Assets

Password is the single most important factor in guarding your digital assets. We need to make our passwords as hard as possible to crack.

Our digital content ranges from access to our e-mail account to sensitive data like bank accounts and personal identity information such as social security number.

It is not just the length of the password, but the combination of letters, special characters and digits that makes it hard to guess for humans and extremely difficult for machines to crack. There are password generators available that can produce such passwords, but they are not human friendly.

Rules for a strong password

We need a password that is not machine generated and yet is hard enough for both humans and machines. A common set of rules for a password that most of us follow is:

  • minimum 8 characters
  • at least 1 digit
  • at least 1 uppercase letter
  • at least 1 lowercase letter
  • at least 1 special character like *,#,@,_ and so on

This scheme has a very high chance to generate a strong password that can keep humans and machines guessing for a very long time. Frequently changing passwords also helps to secure our digital assets.

The Program

We will code a password checker that will return true or false based on the rules listed above.

We will end this post with a password generator program that we will use to verify that our program is functioning correctly.

Let's list the rules.

  1. Minimum 8 characters
  2. At least one letter in uppercase
  3. At least one letter in lowercase
  4. At least one special character

We will write a method to handle one rule and we will bring them all together in a validate() method.

Password Generator

We will use a built-in module to generate a password which will adhere to these rules. We will then use the generated password to verify our implementation.

import string
import secrets

# rule 1
min_chars = 8

# rule 2
upper_case = 1

# rule 3
lower_case = 1

# rule 4
spl_char = 1

specials = ["@", "#", "$", "&", "*", "'"]

# apply rules
def validate(pwd):
    # rule 1: at least 8 characters
    has_chars = has_min_chars(pwd)
    has_upper = has_min_upper(pwd)
    has_lower = has_min_lower(pwd)
    has_spl = has_spl_char(pwd)
    return has_chars == has_upper == has_lower == has_spl

def has_min_chars(pwd):
    return len(pwd) >= min_chars

def has_min_upper(pwd):
     for c in pwd:
         if c == c.upper():
             return True
     return False

def has_min_lower(pwd):
     for c in pwd:
         if c == c.lower():
             return True
     return False

def has_spl_char(pwd):
     for c in pwd:
         if c in specials:
             return True
     return False

def is_valid(pwd):
     return validate(pwd)

p1 = 'Krill$1358'
v1 = is_valid(p1)
print(f'{p1} is valid {v1}')

p2 = 'abcd1234'
v2 = is_valid(p2)
print(f'{p2} is valid {v2}')

p3 = "Sand'2345"
v3 = is_valid(p3)
print(f'{p3} is valid {v3}')

# generate password
length = 15
alphabet = string.ascii_letters + string.digits + string.punctuation

password = ''.join(secrets.choice(alphabet) for i in range(length))
print(password)

p4 = 's[N^m*evKtP{DBW'
v4 = is_valid(p3)
print(f'{p4} is valid {v4}')

Exercise

Extend the rules by adding this -

At least one digit in the password