python快应用:定制强密码

python办公室密码生成簿


每隔一定时间执行下面的代码就可以生成不重复的强密码了!

In [31]:

#coding=utf-8
#  (1) support personal tags 
#  (2) generate every quarter/month and avoid duplication from saved passwords
#  (3) support strong password rules:
#      len >=8 
#      at least one Uppercase, one punctuation, one letter, one digit
import random
import string
pswd_len = 10
#legalize : remove characters easily got confused when typing
legal_lowercase = string.ascii_lowercase.replace('o','')
legal_lowercase = legal_lowercase.replace('l','')
legal_uppercase = string.ascii_uppercase.replace('O','')
legal_digit = string.digits.replace('1','')
legal_digit = legal_digit.replace('0','')
legal_main = legal_lowercase + legal_digit
legal_punc = string.punctuation
print(legal_main)

Output:

abcdefghijkmnpqrstuvwxyz23456789

In [36]:

tag  = 'zt'
pswd = random.sample(legal_main,5)
print(pswd)
pswd.extend(random.sample(legal_digit,1))
print(pswd)
pswd.extend(random.sample(legal_uppercase,1))            
print(pswd)
pswd.extend(random.sample(legal_punc,1))            
print(pswd)

Output:

['q', '3', 'b', 'p', 'x']
['q', '3', 'b', 'p', 'x', '2']
['q', '3', 'b', 'p', 'x', '2', 'C']
['q', '3', 'b', 'p', 'x', '2', 'C', '`']

In [37]:

with open("E:/notes/strongpswd.txt",'a+') as pd:
    for line in pd:
        if pwsd == line :
            print("Duplicated pswd: %s", pswd)
            break  # skip else
    else:
        legal_pswd = tag + ''.join(pswd)
        print(legal_pswd)
        pd.write(legal_pswd+'\n')
    pd.close()

Output:

ztq3bpx2C`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值