Python實現(xiàn)的密碼強(qiáng)度檢測器示例
本文實例講述了Python實現(xiàn)的密碼強(qiáng)度檢測器。分享給大家供大家參考,具體如下:
密碼強(qiáng)度
密碼強(qiáng)度如何量化呢?
一個密碼可以有以下幾種類型:長度、大寫字母、小寫字母、數(shù)字以及特殊符號。
顯然,密碼包含的特征越多、長度越長,其強(qiáng)度也就越高。
我們設(shè)置幾個等級來評測密碼強(qiáng)度,分別是:terrible, simple,
medium, strong。
不同的應(yīng)用可能對密碼強(qiáng)度的要求不一樣,我們引入最小程度(min_length)和最小特征數(shù)(min_types),作為可配置選項。
這樣我們就可以檢測密碼包含的特征,特征與密碼之間的關(guān)系可以簡單定義為:
| 特征數(shù) | 強(qiáng)度 |
|---|---|
| 小于最小長度 | terrible |
| 常用密碼或規(guī)則的密碼 | simple |
| 小于最小特征數(shù) | medium |
| 大于或等于最小特征數(shù) | strong |
另:常用的1萬個密碼點(diǎn)擊此處本站下載。
代碼實現(xiàn)
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()
# 管理密碼強(qiáng)度的類
class Strength(object):
"""
密碼強(qiáng)度三個屬性:是否有效valid, 強(qiáng)度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", "密碼有規(guī)則")
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", "密碼還不夠強(qiáng)")
return Strength(True, "strong", "密碼很強(qiáng)")
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('規(guī)則' in rv.message)
def test_by_step(self):
rv = check.password("abcdefg")
self.assertTrue(repr(rv) == "simple")
self.assertTrue('規(guī)則' 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('不夠強(qiáng)' in rv.message)
def test_strong(self):
rv = check.password("asjka9AD")
self.assertTrue(repr(rv) == 'strong')
self.assertTrue('很強(qiáng)' 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()
PS:這里再為大家提供兩款相關(guān)在線工具供大家參考使用:
在線隨機(jī)數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高強(qiáng)度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python selenium 自動化腳本打包成一個exe文件(推薦)
這篇文章主要介紹了Python selenium 自動化腳本打包成一個exe文件,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟
這篇文章主要介紹了django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
在Django中管理Users和Permissions以及Groups的方法
這篇文章主要介紹了在Django中管理Users和Permissions以及Groups的方法,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下2015-07-07

