python 生成包含数字、大写字母、小写字母和特殊字符的密码,并且使用bcrypt对密码进行加密

本文介绍如何使用Python的`bcrypt`库生成包含数字、字母、特殊字符的随机密码,并确保每条密码独一无二。同时演示了如何对生成的密码进行哈希加密,提高安全性。
import bcrypt
import random
import string


def print_passwords():
    str1 = "!@#$%^&*"
    src = string.ascii_letters + string.digits + str1
    count = input('请确认要生成几条密码: ')
    p = input('请确认要生成的密码位数(最少4位): ')
    list_passwords = []
    for i in range(int(count)):
        while 1 == 1:
            list_passwords_all = random.choices(src, k=int(p) - 4)
            list_passwords_all.extend(random.sample(string.digits, 1))  # 让密码中一定包含数字
            list_passwords_all.extend(random.sample(string.ascii_lowercase, 1))  # 让密码中一定包含小写字母
            list_passwords_all.extend(random.sample(string.ascii_uppercase, 1))  # 让密码中一定包含大写字母
            list_passwords_all.extend(random.sample(str1, 1))  # 让密码中一定包含特殊符号
            random.shuffle(list_passwords_all)  # 打乱列表顺序
            str_passwd = ''.join(list_passwords_all)  # 将列表转化为字符串
            if str_passwd not in list_passwords:  # 判断是否生成重复密码
                list_passwords.append(str_passwd)
                break
    print(list_passwords)
    for passwd in list_passwords:
        salt = bcrypt.gensalt()
        hashed = bcrypt.hashpw(passwd.encode(), salt)
        print(passwd)
        # print(salt)
        print(hashed)


if __name__ == '__main__':
    print_passwords()
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值