密码强度小实例,主要是为了学习类的知识,希望指正有什么问题

自己做的一个密码强度的小实例,希望有看到的同学给指正有什么不足的地方

​
"""
    作者: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()

​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值