Python實現的密碼強度檢測器示例
本文實例講述了Python實現的密碼強度檢測器。分享給大家供大家參考,具體如下:
密碼強度
密碼強度如何量化呢?
一個密碼可以有以下幾種類型:長度、大寫字母、小寫字母、數字以及特殊符號。
顯然,密碼包含的
特征越多、長度越長,其強度也就越高。
我們設置幾個等級來評測密碼強度,分別是:terrible, simple,
medium, strong。
不同的應用可能對密碼強度的要求不一樣,我們引入最小程度(min_length)和最小
特征數(min_types),作為可配置選項。
這樣我們就可以檢測密碼包含的
特征,
特征與密碼之間的關系可以簡單定義為:

另:常用的1萬個密碼點擊此處本站下載。
代碼實現
check.py
# coding: utf-8
"""
check
Check if your password safe
"""
import re
#
特征
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 words
COMMON_WORDS = load_common_password()
# 管理密碼強度的類
class Strength(object):
"""
密碼強度三個屬性:是否有效valid, 強度strength, 提示信息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.valid
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", "密碼太短了")
if self.is_regular(input) or self.is_by_step(input):
return Strength(False, "simple", "密碼有規則")
if self.is_common(input):
return Strength(False, "simple", "密碼很常見")
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", "密碼太簡單了")
if types < min_type:
return Strength(level <= self.MEDIUM, "medium", "密碼還不夠強")
return Strength(True, "strong", "密碼很強")
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', '163', 'gmail', '126', '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: 用于單元測試
# coding: utf-8
"""
test for check
"""
import unittest
import check
class TestCheck(unittest.TestCase):
def test_regular(self):
rv = check.password("qwerty")
self.assertTrue(repr(rv) == "simple")
self.assertTrue('規則' in rv.message)
def test_by_step(self):
rv = check.password("abcdefg")
self.assertTrue(repr(rv) == "simple")
self.assertTrue('規則' in rv.message)
def test_common(self):
rv = check.password("password")
self.assertTrue(repr(rv) == "simple")
self.assertTrue('常見' in rv.message)
def test_medium(self):
rv = check.password("ahj01a")
self.assertTrue(repr(rv) == 'medium')
self.assertTrue('不夠強' in rv.message)
def test_strong(self):
rv = check.password("asjka9AD")
self.assertTrue(repr(rv) == 'strong')
self.assertTrue('很強' in rv.message)
# 測試郵箱
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', '163', 'gmail', '126', 'sina']
self.assertIn(rv.get_email_type(), types)
if __name__ == '__main__':
unittest.main()
CDA數據分析師考試相關入口一覽(建議收藏):
? 想報名CDA認證考試,點擊>>>
“CDA報名”
了解CDA考試詳情;
? 想學習CDA考試教材,點擊>>> “CDA教材” 了解CDA考試詳情;
? 想加入CDA考試題庫,點擊>>> “CDA題庫” 了解CDA考試詳情;
? 想了解CDA考試含金量,點擊>>> “CDA含金量” 了解CDA考試詳情;