SoFunction
Updated on 2024-11-07

Example of a password strength checker implemented in Python

This article is an example of a password strength checker implemented in Python. Shared for your reference, as follows:

Password strength

How is password strength quantified?

A password can be of the following types: length, uppercase letters, lowercase letters, numbers, and special symbols.

Obviously, the more features and the longer the length of a password contains, the stronger it will be.

We set several levels to rate password strength, which are:terrible, simple,
medium, strong

Different applications may have different requirements for password strength, and we introduce minimum degree (min_length) and minimum number of features (min_types) as configurable options.

In this way we can detect the features contained in the password and the relationship between the features and the password can be simply defined as:

characteristic number (math.) dissociation
Less than minimum length terrible
Passwords for common passwords or rules simple
Less than the minimum number of features medium
Greater than or equal to the minimum number of features strong

Another:For commonly used 10,000 passwords click hereDownload

code implementation

# coding: utf-8
"""
check
Check if your password safe
"""
import re
# Features
NUMBER = (r'[0-9]')
LOWER_CASE = (r'[a-z]')
UPPER_CASE = (r'[A-Z]')
OTHERS = (r'[^0-9A-Za-z]')
def load_common_password():
 words = []
 with open("10k_most_common.txt", "r") as f:
  for word in f:
   (())
 return words
COMMON_WORDS = load_common_password()
# Classes for managing password strength
class Strength(object):
 """
 Three attributes of password strength: validity, strength, message
 """
 def __init__(self, valid, strength, message):
   = valid
   = strength
   = message
 def __repr__(self):
  return 
 def __str__(self):
  return 
 def __bool__(self):
  return 
class Password(object):
 TERRIBLE = 0
 SIMPLE = 1
 MEDIUM = 2
 STRONG = 3
 @staticmethod
 def is_regular(input):
  regular = ''.join(['qwertyuiop', 'asdfghjkl', 'zxcvbnm'])
  return input in regular or input[::-1] in regular
 @staticmethod
 def is_by_step(input):
  delta = ord(input[1]) - ord(input[0])
  for i in range(2, len(input)):
   if ord(input[i]) - ord(input[i - 1]) != delta:
    return False
  return True
 @staticmethod
 def is_common(input):
  return input in COMMON_WORDS
 def __call__(self, input, min_length=6, min_type=3, level=STRONG):
  if len(input) < min_length:
   return Strength(False, "terrible", "The password is too short.")
  if self.is_regular(input) or self.is_by_step(input):
   return Strength(False, "simple", "Passwords have rules.")
  if self.is_common(input):
   return Strength(False, "simple", "Passwords are common.")
  types = 0
  if (input):
   types += 1
  if LOWER_CASE.search(input):
   types += 1
  if UPPER_CASE.search(input):
   types += 1
  if (input):
   types += 1
  if types < 2:
   return Strength(level <= , "simple", "The password is too simple.")
  if types < min_type:
   return Strength(level <= , "medium", "The code isn't strong enough.")
  return Strength(True, "strong", "The code is strong.")
class Email(object):
 def __init__(self, email):
   = email
 def is_valid_email(self):
  if ("^.+@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", ):
   return True
  return False
 def get_email_type(self):
  types = ['qq', '163', 'gmail', '126', 'sina']
  email_type = ('@\w+', ).group()[1:]
  if email_type in types:
   return email_type
  return 'wrong email'
password = Password()

test_check.py: for unit tests

# coding: utf-8
"""
test for check
"""
import unittest
import check
class TestCheck():
 def test_regular(self):
  rv = ("qwerty")
  (repr(rv) == "simple")
  ('Rules' in )
 def test_by_step(self):
  rv = ("abcdefg")
  (repr(rv) == "simple")
  ('Rules' in )
 def test_common(self):
  rv = ("password")
  (repr(rv) == "simple")
  ('Common' in )
 def test_medium(self):
  rv = ("ahj01a")
  (repr(rv) == 'medium')
  ('Not strong enough' in )
 def test_strong(self):
  rv = ("asjka9AD")
  (repr(rv) == 'strong')
  ('Very strong' in )
 # Test Mailbox
 def test_email(self):
  rv = ("123@")
  (rv.is_valid_email(), True)
 def test_email_type(self):
  rv = ("123@")
  types = ['qq', '163', 'gmail', '126', 'sina']
  (rv.get_email_type(), types)
if __name__ == '__main__':
 ()

PS: Here are two more related online tools for your reference:

Online random number/string generation tool:
http://tools./aideddesign/suijishu

High strength password generator:
http://tools./password/CreateStrongPassword

Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.