The examples in this article share the specific code for python to implement password strength verification for your reference. The specific content is as follows
One verification rule
Rule 1 Password length is more than 8 characters
Rule 2 The password must contain numbers
Rule 3 The password must contain uppercase and lowercase letters
Rule 4 The password must contain special characters ['+','-','*','/','_','&','%',',']
Rule 5 If the verification fails 5 times, it will be forced to exit
Two file operations
Each password entered will be saved to a text file
The following is the python code implementation:
"""
Author: zhengzhihui
Version: 7.0
Date: 2019/7/13
Function: Determine the password strength
2.0 Function: loop and terminate
3.0 Function: save the password in the text
4.0 Function: read files, traverse files
5.0 Function: Define PasswordTool class
6.0 Function: Define the FileTool class
7.0 Function: add uppercase and lowercase letters and special characters in the password['+','-','*','/','_','&','%',',']"""
import time as tm
classFileTool():"""
File tools
"""
def __init__(self, filepath):
self.filepath = filepath
def write_to_file(self, content):withopen(self.filepath,'a')as f:
f.write(content)
def read_from_file(self):withopen(self.filepath,'r')as f:
content = f.readlines()return content
classPasswordTool():"""
Password tools
"""
def __init__(self, password):
self.password = password
self.strength_level =0
def check_number_exist(self):"""
Determine whether it contains numbers
"""
has_number = False
for c in self.password:if c.isnumeric():
has_number = True
breakreturn has_number
def check_letter_exist(self):"""
Determine whether it contains letters
"""
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:breakreturn has_both_letter
def check_specialchar_exist(self):"""
Determine whether it contains special characters
"""
has_specialchar = False
specialchar_list =['+','-','*','/','_','&','%',',']for c in self.password:if c in specialchar_list:
has_specialchar = True
breakreturn has_specialchar
def process_password(self):"""
Determine whether it meets the rules
"""
# Rule 1: At least 8 bits in length
iflen(self.password)=8:
self.strength_level +=1else:print('Password length is at least 8 characters')
# Rule 2: Must contain numbers
if self.check_number_exist():
self.strength_level +=1else:print('Password needs to contain numbers')
# Rule 3: Must contain uppercase and lowercase letters
if self.check_letter_exist():
self.strength_level +=1else:print('Password must contain uppercase and lowercase letters')
# Rule 4: Special characters need to be included
if self.check_specialchar_exist():
self.strength_level +=1else:print('Password must contain at least one special character("+,-,*,/,_")')
def main():"""
Main function
"""
try_times =5
pwd_strength_dict ={0:'weak',1:'较weak',2:'in',3:'Strong',4:'超Strong'}
myfile =FileTool("password_7.0.txt")while try_times 0:
password =input('Please enter password: ')
mypwdtool =PasswordTool(password)
mypwdtool.process_password()
now_time = tm.strftime("%Y-%m-%d %H:%M:%S", tm.localtime())
myfile.write_to_file("date:{}password:{}strength:{}{}\n".format(now_time, password,
mypwdtool.strength_level, pwd_strength_dict[mypwdtool.strength_level]))if mypwdtool.strength_level =4:print('Congratulations! Password qualified')breakelse:print('Unqualified password')
try_times -=1print()if try_times <=0:print('Too many attempts, password setting failed!')
content = myfile.read_from_file()print(content)if __name__ =="__main__":main()
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts