自己做的一个密码强度的小实例,希望有看到的同学给指正有什么不足的地方
"""
作者:wpf
功能:判断密码强弱
版本:v1.0
时间:2020年7月5日-2020年7月6日
内容:根据判断密码强弱继续进行添加内容,加入更加复杂的密码设置规则:
1.同时包含大小写字母 区分开为2个条件
2.必须包含特殊字符,如:+,-, *,等
延伸正则表达式的内容
"""
# 密码类
class PasswordTool:
# 类的属性
def __init__(self, password):
self.password = password
self.length = 0
# 进行判断的方法
def process_password(self):
if self.length_password():
self.length += 1
else:
print("密码强度不足8位")
if self.password_number_check():
self.length += 1
else:
print("密码中没有数字")
if self.password_letter_check():
self.length += 1
else:
print("密码中没有字母")
if self.password_special_check():
self.length += 1
else:
print("密码未添加特殊字符")
if self.password_upper_check():
self.length += 1
else:
print("密码内没有大写")
if self.password_lower_check():
self.length += 1
else:
print("密码内没有小写")
# 判断 密码长度
def length_password(self):
has_length = False
if len(self.password) >= 8:
has_length = True
return has_length
# 判断是否含有数字
def password_number_check(self):
has_number = False
for n in self.password:
if n.isnumeric():
has_number = True
break
return has_number
# 判断是否含有字母
def password_letter_check(self):
has_letter = False
for l in self.password:
if l.isalpha():
has_letter = True
break
return has_letter
# 判断是否含有特殊符号
def password_special_check(self):
has_special = False
# 定义我们需要的特殊字符
special = "!@#$%^&*()_+-{}|<>?.,/"
for s in self.password:
if s in special:
has_special = True
break
return has_special
# 判断是否含有大写字符
def password_upper_check(self):
has_upper = False
for o in self.password:
if o.isupper():
has_upper = True
return has_upper
# 判断是否含有小写字符
def password_lower_check(self):
has_lower = False
for o in self.password:
if o.islower():
has_lower = True
return has_lower
# 文件类
class FileTool:
def __init__(self, filepath):
self.filepath = filepath
# 写入文件
def write_to_file(self, line):
f = open(self.filepath, "w")
f.write(line)
f.close()
# 读取文件
def read_from_file(self):
f = open(self.filepath, "r")
lines = f.readlines()
f.close()
return lines
# 主函数
def main():
# 定义文件地址
filepath = "password_v1.0.txt"
# 限制输入次数
try_times = 5
# 文件类实例化
file_tool = FileTool(filepath)
while try_times >= 0:
password = input("请输入密码")
# 密码类实例化
password_tool = PasswordTool(password)
password_tool.process_password()
line = "密码:{} ,强度:{}/n".format(password, password_tool.length)
file_tool.write_to_file(line)
lines = file_tool.read_from_file()
print(lines)
if password_tool.length == 6:
print("密码合格")
break
else:
print("密码不合格")
try_times -= 1
print()
if try_times == 0:
print("实验超过5次")
lines = file_tool.read_from_file()
print(lines)
# 入口
if __name__ == "__main__":
main()
240

被折叠的 条评论
为什么被折叠?



