Python implements password strength verification

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

Python implements password strength verification
Python implements username and password verification
Python implements verification code recognition
Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Python implements the brick-and-mortar game
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down
Python verification code interception identification code example