Python實(shí)現(xiàn)的密碼強(qiáng)度檢測(cè)器示例
本文實(shí)例講述了Python實(shí)現(xiàn)的密碼強(qiáng)度檢測(cè)器。分享給大家供大家參考,具體如下:
密碼強(qiáng)度
密碼強(qiáng)度如何量化呢?
一個(gè)密碼可以有以下幾種類型:長(zhǎng)度、大寫字母、小寫字母、數(shù)字以及特殊符號(hào)。
顯然,密碼包含的特征越多、長(zhǎng)度越長(zhǎng),其強(qiáng)度也就越高。
我們?cè)O(shè)置幾個(gè)等級(jí)來評(píng)測(cè)密碼強(qiáng)度,分別是:terrible, simple,
medium, strong。
不同的應(yīng)用可能對(duì)密碼強(qiáng)度的要求不一樣,我們引入最小程度(min_length)和最小特征數(shù)(min_types),作為可配置選項(xiàng)。
這樣我們就可以檢測(cè)密碼包含的特征,特征與密碼之間的關(guān)系可以簡(jiǎn)單定義為:
特征數(shù) | 強(qiáng)度 |
---|---|
小于最小長(zhǎng)度 | terrible |
常用密碼或規(guī)則的密碼 | simple |
小于最小特征數(shù) | medium |
大于或等于最小特征數(shù) | strong |
另:常用的1萬個(gè)密碼點(diǎn)擊此處本站下載。
代碼實(shí)現(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)度三個(gè)屬性:是否有效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", "密碼太簡(jiǎn)單了") 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: 用于單元測(cè)試
# 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) # 測(cè)試郵箱 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文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python wordcloud庫實(shí)例講解使用方法
這篇文章主要介紹了python wordcloud庫實(shí)例,詞云通過以詞語為基本單位,更加直觀和藝術(shù)地展示文本。wordcloud是優(yōu)秀的詞云展示的python第三方庫2022-12-12Python selenium 自動(dòng)化腳本打包成一個(gè)exe文件(推薦)
這篇文章主要介紹了Python selenium 自動(dòng)化腳本打包成一個(gè)exe文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟
這篇文章主要介紹了django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02在Django中管理Users和Permissions以及Groups的方法
這篇文章主要介紹了在Django中管理Users和Permissions以及Groups的方法,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下2015-07-07Python一行代碼實(shí)現(xiàn)快速排序的方法
排序算法是在高考或中考中出現(xiàn)頻率最多的點(diǎn),所以大家要掌握,今天小編給大家?guī)砹送ㄟ^Python一行代碼實(shí)現(xiàn)快速排序的方法,感興趣的朋友跟隨小編一起看看吧2019-04-04