Python-based password strength detector example, python Detector
This article describes the Python-implemented password strength detector. We will share this with you for your reference. The details are as follows:
Password strength
How can I quantify the password strength?
A password can be of the following types: length, uppercase letters, lowercase letters, numbers, and special characters.
Obviously, the more features the password contains and the longer the length, the higher the strength.
We set several levels to evaluate the password strength: terrible, simple,
Medium, strong.
Different applications may have different requirements on password strength. We introduce min_length and min_types as configurable options.
In this way, we can detect the features of passwords. The relationship between features and passwords can be defined:
Feature count |
Strength |
Less than minimum length |
Terrible |
Password of common passwords or rules |
Simple |
Less than the minimum number of features |
Medium |
Minimum number of features greater than or equal |
Strong |
In addition:Click here for the commonly used 10 thousand passwordsDownload from this site.
Code Implementation
Check. py
# Coding: UTF-8 "checkCheck if your password safe" import re # feature NUMBER = re. compile (R' [0-9] ') LOWER_CASE = re. compile (R' [a-z] ') UPPER_CASE = re. compile (R' [A-Z] ') OTHERS = re. compile (R' [^ 0-9A-Za-z] ') def load_common_password (): words = [] with open ("10k_most_common.txt", "r") as f: for word in f: words. append (word. strip () return wordsCOMMON_WORDS = load_common_password () # class for managing password Strength (objec T): "password strength three attributes: valid, strength, prompt message" "def _ init _ (self, valid, strength, message): self. valid = valid self. strength = strength self. message = message def _ repr _ (self): return self. strength def _ str _ (self): return self. message def _ bool _ (self): return self. validclass 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", "password Rules") if self. is_common (input): return Strength (False, "simple", "very common passwords ") Types = 0 if NUMBER. search (input): types + = 1 if LOWER_CASE.search (input): types + = 1 if UPPER_CASE.search (input): types + = 1 if OTHERS. search (input): types + = 1 if types <2: return Strength (level <= self. SIMPLE, "simple", "the password is too SIMPLE") if types <min_type: return Strength (level <= self. MEDIUM, "medium", "the password is not strong enough") return Strength (True, "strong", "strong Password") class Email (object ): def _ init _ (self, email ): Self. email = email def is_valid_email (self): if re. match ("^. + @(\\[?) [A-zA-Z0-9 \-\.] + \. ([a-zA-Z] {2, 3} | [0-9] {1, 3}) (\]?) $ ", Self. email): return True return False def get_email_type (self): types = ['qq', '20170101', 'gmail ', '20160301', 'sina'] email_type = re. search ('@ \ w +', self. email ). group () [1:] if email_type in types: return email_type return 'wrong email 'password = Password ()
Test_check.py: used for unit testing
# Coding: UTF-8 "test for check" "import unittestimport checkclass TestCheck (unittest. testCase): def test_regular (self): rv = check. password ("qwerty") self. assertTrue (repr (rv) = "simple") self. assertTrue ('rule' in rv. message) def test_by_step (self): rv = check. password ("abcdefg") self. assertTrue (repr (rv) = "simple") self. assertTrue ('rule' in rv. message) def test_common (self): rv = check. password ("password") self. assertTrue (repr (rv) = "simple") self. assertTrue ('common' in rv. message) def test_medium (self): rv = check. password ("ahj01a") self. assertTrue (repr (rv) = 'medium ') self. assertTrue ('not strong enough 'in rv. message) def test_strong (self): rv = check. password ("asjka9AD") self. assertTrue (repr (rv) = 'strong ') self. assertTrue ('strong 'in rv. message) # test email def test_email (self): rv = check. email ("123@gmail.com") self. assertEqual (rv. is_valid_email (), True) def test_email_type (self): rv = check. email ("123@gmail.com") types = ['qq', '123456', 'gmail ', '123456', 'sina'] self. assertIn (rv. get_email_type (), types) if _ name _ = '_ main _': unittest. main ()
PS: Here are two related online tools for your reference:
Online random number/string generation tool:
Http://tools.jb51.net/aideddesign/suijishu
High-strength Password generator:
Http://tools.jb51.net/password/CreateStrongPassword