python - 密码生成器有效密码检查
问题描述
我正在用字典构建自己的密码生成器,并检查里面是否有每种类型的字符。它工作正常,但我认为我对支票的编码有点复杂。
如果有办法以更好的方式对此进行编码,您是否有想法。如果它已经在较低的位置上,有没有办法摆脱检查,所以它不检查其他类型?
PS:我希望我自己定义使用的下限/上限/特价/数字,这样我就可以始终避免添加我不喜欢的字符。
chars = ""
alpha_lowers = "abcdefghijklmnopqrstuvwxyz"
alpha_uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
specials = "$%&/()=?.,"
nums = "0123456789"
dictionary = {
"a" : "anton",
"b" : "berta",
"c" : "caesar",
"d" : "dora",
"e" : "emil",
"f" : "friedich",
"g" : "gustav",
"h" : "hotel",
"i" : "india",
"j" : "julia",
"k" : "kilo",
"l" : "ludwig",
"m" : "marta",
"n" : "nordpol",
"o" : "otto",
"p" : "paula",
"q" : "quelle",
"r" : "richard",
"s" : "iegfried",
"t" : "theodor",
"u" : "ulrich",
"v" : "viktor",
"w" : "willhelm",
"x" : "xaver",
"y" : "ypsilon",
"z" : "zeppelin",
"A" : "Anton",
"B" : "Berta",
"C" : "Caesar",
"D" : "Dora",
"E" : "Emil",
"F" : "Friedrich",
"G" : "Golf",
"H" : "Hotel",
"I" : "India",
"J" : "Julius",
"K" : "Kilo",
"L" : "Ludwig",
"M" : "Marta",
"N" : "Nordpol",
"O" : "Otto",
"P" : "Paula",
"Q" : "Quelle",
"R" : "Richard",
"S" : "Siegfried",
"T" : "Theodor",
"U" : "Ulrich",
"V" : "Viktor",
"W" : "Willhelm",
"X" : "Xaver",
"Y" : "Ypsilon",
"Z" : "Zeppelin",
"$" : "Dollar",
"%" : "Prozent",
"&" : "Und",
"/" : "Schräg",
"(" : "Klammer auf",
")" : "Klammer zu",
"=" : "Gleich",
"?" : "Fragezeichen",
"." : "Punkt",
"," : "Beistrich",
"0" : "Null",
"1" : "Eins",
"2" : "Zwei",
"3" : "Drei",
"4" : "Vier",
"5" : "Fünf",
"6" : "Sechs",
"7" : "Sieben",
"8" : "Acht",
"9" : "Neun"
}
all_chars = True
# Kleinbuchstaben hinzufügen // Adding Lowers
chars = chars + alpha_lowers
# Großbuchstaben hinzufügen // Adding uppers
chars = chars + alpha_uppers
# Spezial-Zeichen hinzufügen // Adding Specials
chars = chars + specials
# Nummern hinzufügen // Adding Nums
chars = chars + nums
# PW-Menge definieren // How many PW
password_n = 10
# PW-Länge definieren // Password length
password_len = 32
#--------------------------------------------------------------
def password_gen(length):
# Generating PW
password = ""
for i in range (0, length):
password = password + random.choice(chars)
# Check if there is a Char from every type
if all_chars == True:
in_alpha_lowers = False
in_alpha_uppers = False
in_specials = False
in_nums = False
for c in password:
if in_alpha_lowers == False:
if c in alpha_lowers:
in_alpha_lowers = True
if in_alpha_uppers == False:
if c in alpha_uppers:
in_alpha_uppers = True
if in_specials == False:
if c in specials:
in_specials = True
if in_nums == False:
if c in nums:
in_nums = True
if in_alpha_lowers == False or in_alpha_uppers == False or in_specials == False or in_nums == False:
print(password + " is not valid! New Passwort will be generated!" + "\n")
return "invalid"
else:
return password
else:
return password
#--------------------------------------------------------------
i = 1
while i <= password_n:
password = ""
sentence = ""
password = password_gen(password_len)
if password != "invalid":
print("valid Passwort")
i += 1
for c in password:
sentence = sentence + " " + dictionary[c]
print(password)
print(sentence.lstrip() + "\n")
解决方案
如果有办法以更好的方式对此进行编码,您是否有想法。
我建议看看它set
的操作intersect
。例如,您可以通过以下方式检查密码是否包含小写字母:
alpha_lowers = "abcdefghijklmnopqrstuvwxyz"
password = "password"
haslower = bool(set(password).intersection(alpha_lowers))
print(haslower) # True
说明:我确实将一组字母与alpha_lowers
and共用password
,然后将其转换为 bool,这导致False
ifset
为空,True
否则为空。
如果它已经在较低的位置上,有没有办法摆脱检查,所以它不检查其他类型?
由于您已经拥有功能,因此您可能会return "invalid"
在检查未通过后立即进行操作。
推荐阅读
- api - 通过 API 的 OAuth2.0 连接失败
- javascript - 在画布上用渐变画线
- angular - CkEditor 5 自定义拼写检查
- node.js - 如何在继续之前等待异步 Mongoose Promise 返回
- pyspark - pyspark - 转换 DF 结构
- c++ - 来自 mingw-w64 的 wxWidgets 不能在 CLion 中使用
- node.js - 如何使用 lambda 函数将一个 S3 存储桶中的 .m3u8 文件及其 .ts 文件复制到另一个存储桶
- c# - 当参数是插值字符串时,是否可以默认调用方法(FormattableString x)重载?
- shell - 可视化命令的子shell/进程
- javascript - 在嵌套 js 中创建自定义验证器