python實現(xiàn)密碼強度校驗
本文實例為大家分享了python實現(xiàn)密碼強度校驗的具體代碼,供大家參考,具體內(nèi)容如下
一 校驗規(guī)則
規(guī)則1 密碼長度8位以上
規(guī)則2 密碼需包含數(shù)字
規(guī)則3 密碼需包含大小寫字母
規(guī)則4 密碼需包含特殊字符['+', '-', '*', '/', '_', '&', '%', ',']
規(guī)則5 校驗5次不通過則強制退出
二 文件操作
每次輸入的密碼都會保存到文本文件中
以下是python的代碼實現(xiàn):
"""
作者:zhengzhihui
版本:7.0
日期:2019/7/13
功能:判斷密碼強度
2.0功能:循環(huán)和終止
3.0功能:將密碼保存到文本中
4.0功能:讀取文件,遍歷文件
5.0功能:定義PasswordTool類
6.0功能:定義FileTool類
7.0功能:密碼中增加大小寫字母和特殊字符['+', '-', '*', '/', '_', '&', '%', ',']
"""
import time as tm
class FileTool():
"""
文件工具類
"""
def __init__(self, filepath):
self.filepath = filepath
def write_to_file(self, content):
with open(self.filepath, 'a') as f:
f.write(content)
def read_from_file(self):
with open(self.filepath, 'r') as f:
content = f.readlines()
return content
class PasswordTool():
"""
密碼工具類
"""
def __init__(self, password):
self.password = password
self.strength_level = 0
def check_number_exist(self):
"""
判斷是否含數(shù)字
"""
has_number = False
for c in self.password:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(self):
"""
判斷是否含字母
"""
has_upper_letter = False
has_lower_letter = False
for c in self.password:
if c.isupper():
has_upper_letter = True
elif c.islower():
has_lower_letter = True
has_both_letter = has_upper_letter and has_lower_letter
if has_both_letter:
break
return has_both_letter
def check_specialchar_exist(self):
"""
判斷是否包含特殊字符
"""
has_specialchar = False
specialchar_list = ['+', '-', '*', '/', '_', '&', '%', ',']
for c in self.password:
if c in specialchar_list:
has_specialchar = True
break
return has_specialchar
def process_password(self):
"""
判斷是否符合規(guī)則
"""
# 規(guī)則1:長度至少8位
if len(self.password) >= 8:
self.strength_level += 1
else:
print('密碼長度至少8位')
# 規(guī)則2:必須包含數(shù)字
if self.check_number_exist():
self.strength_level += 1
else:
print('密碼需要包含數(shù)字')
# 規(guī)則3:必須包含大小寫字母
if self.check_letter_exist():
self.strength_level += 1
else:
print('密碼需要包含大小寫字母')
# 規(guī)則4:需要包含特殊字符
if self.check_specialchar_exist():
self.strength_level += 1
else:
print('密碼需要包含至少一個特殊字符("+,-,*,/,_")')
def main():
"""
主函數(shù)
"""
try_times = 5
pwd_strength_dict = {0: '弱', 1: '較弱', 2: '中', 3: '強', 4: '超強'}
myfile = FileTool("password_7.0.txt")
while try_times > 0:
password = input('請輸入密碼: ')
mypwdtool = PasswordTool(password)
mypwdtool.process_password()
now_time = tm.strftime("%Y-%m-%d %H:%M:%S", tm.localtime())
myfile.write_to_file("日期:{} 密碼:{} 強度:{}{}\n".format(now_time, password,
mypwdtool.strength_level, pwd_strength_dict[mypwdtool.strength_level]))
if mypwdtool.strength_level >= 4:
print('恭喜!密碼合格')
break
else:
print('密碼不合格')
try_times -= 1
print()
if try_times <= 0:
print('嘗試次數(shù)過多,密碼設(shè)置失敗!')
content = myfile.read_from_file()
print(content)
if __name__ == "__main__":
main()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python(wordcloud)如何根據(jù)文本數(shù)據(jù)(.txt文件)繪制詞云圖
這篇文章主要給大家介紹了關(guān)于Python(wordcloud)如何根據(jù)文本數(shù)據(jù)(.txt文件)繪制詞云圖的相關(guān)資料,詞云Wordcloud是文本數(shù)據(jù)的一種可視化表示方式,它通過設(shè)置不同的字體大小或顏色來表現(xiàn)每個術(shù)語的重要性,需要的朋友可以參考下2024-05-05
Django cookie和session的應(yīng)用場景及如何使用
今天我們來重點看下Django中session和cookie的用法吧。我們會介紹cookie和session的工作原理,還會分享實際應(yīng)用的案例。2021-04-04
python數(shù)據(jù)預(yù)處理 :數(shù)據(jù)抽樣解析
這篇文章主要介紹了python數(shù)據(jù)預(yù)處理 :數(shù)據(jù)抽樣解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python腳本生成caffe train_list.txt的方法
下面小編就為大家分享一篇python腳本生成caffe train_list.txt的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python用于url解碼和中文解析的小腳本(python url decoder)
這篇文章主要介紹了python用于url解碼和中文解析的代碼,需要的朋友可以參考下2013-08-08
在python Numpy中求向量和矩陣的范數(shù)實例
今天小編就為大家分享一篇在python Numpy中求向量和矩陣的范數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

