python实战——密码生成器

需求

日常生活中因为工作或生活的需要,我们注册了一堆账号,需要创建一堆密码。有的人可能图省事所有账号的密码都是一样的,这种做法非常不安全,一旦有个账号或对应的平台出现问题,就非常麻烦,但是 每个账号都去创建新的密码,想密码也是非常让人头痛的问题。

我们本篇实在的目的就是使用python实现一个密码生成器,分成两种情况,第一种:对密码的组成字母、数字、特殊字符有比例要求(类中固定比例为字母——数字——特殊字符:5:3:2),第二种 对密码的组成及比例没有要求,随机生成就好,让我们一起来实现这个小功能吧。

实现

实现类为PasswordGenerator,有三个静态字段ALPHA、NUMBER、SPECIAL分别表示待选的字母,数字以及特殊字符,这里偷懒没有放大写字母,有兴趣的朋友可以把它加上。有两个属性password_length,password分别表示密码长度和最终生成的密码。有一个静态方法generate_pass,功能为根据候选字符串和需要生成的字符串长度随机生成给定长度的字符串。两个方案fixed_proportion_generate、random_generate,分别表示生成固定组成比例的密码字符串和随机密码字符串。

类的具体实现如下,为了便于读者理解,尽量都加了注释:

class PasswordGenerator:
    # 候选字母
    ALPHA = "abcdefghijklmnopqrstuvwxyz"
    # 候选数字
    NUMBER = "0123456789"
    # 候选特殊字符
    SPECIAL = "@#$%&*"

    def __init__(self, password_length=8):
        """
        初始化
        :param password_length:
        密码长度,默认为8
        """
        self.password_length = password_length
        # 存放生成的密码
        self.password = []

    def random_generate(self):
        """
        密码字符、数字、以及特殊字符的比例是随机来生成密码
        :return:
        """
        #  string.ascii_letters :ASCII码,包括字母的大小写(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz)
        #  string.digits :数字字符串 0123456789
        #  string.punctuation :特殊字符字符串 !"#$%&'()*+,-./:;<=>?@[\]^_{|}~
        total = string.ascii_letters + string.digits + string.punctuation
        return "".join(random.sample(total, self.password_length))

    def fixed_proportion_generate(self):
        """
        固定字母——数字——特殊字符的比例
        这里是按照5——3——2的比例来生成密码
        :return:
        """
        alpha_len = self.password_length // 2
        num_len = math.ceil(self.password_length * 30 / 100)
        special_len = self.password_length - (alpha_len + num_len)
        # 生成密码的字母部分
        self.password.append(PasswordGenerator.generate_pass(alpha_len, PasswordGenerator.ALPHA, True))
        # 生成密码的数字部分
        self.password.append(PasswordGenerator.generate_pass(num_len, PasswordGenerator.NUMBER))
        # 生成密码的特殊字符部分
        self.password.append(PasswordGenerator.generate_pass(special_len, PasswordGenerator.SPECIAL))
        gen_password = []
        for i in self.password:
            gen_password.extend(i)
        # 随机打乱生成的密码顺序
        random.shuffle(gen_password)
        # 连接成字符串返回
        return "".join(gen_password)

    @staticmethod
    def generate_pass(length, array, is_alpha=False):
        password = []
        for i in range(length):
            index = random.randint(0, len(array) - 1)
            character = array[index]
            if is_alpha:
                case = random.randint(0, 1)
                if case == 1:
                    character = character.upper()
            password.append(character)
        return password

接下来我们来使用这个类来生成密码,代码如下:

if __name__ == '__main__':

    # 使用密码生成器
    pass_len = int(input("请输入密码长度:"))
    is_fixed = bool(input("是否固定比例(true/false):"))
    generator = PasswordGenerator(pass_len)
    gen_password = ''
    if is_fixed:
        gen_password =generator.fixed_proportion_generate()
    else:
        gen_password = generator.random_generate()
    print(gen_password)

最终效果如下:

# 固定比例
>>>请输入密码长度:12
>>>是否固定比例(true/false):true
>>>1HT&6$Fox1B4
# 非固定比例
>>>请输入密码长度:10
>>>是否固定比例(true/false):false
>>>k03$aOM$z5

欢迎大家留意交流哈

猜你喜欢

转载自blog.csdn.net/chen565884393/article/details/127101886