每隔一定时间执行下面的代码就可以生成不重复的强密码了!
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`