Python 密码生成器有效密码检查
我正在用字典构建自己的密码生成器,并检查里面是否有每种类型的字符。它工作正常,但我想我把支票编码得有点复杂 如果有更好的编码方法,你有什么想法吗。 有没有办法从检查中解脱出来,如果它在下降中已经准备好了,那么它就不会检查其他类型了 注:我想自己定义一下ussed lowers/uppers/specials/NUM,这样我就可以避免添加我不喜欢的charsPython 密码生成器有效密码检查,python,random,passwords,generator,change-password,Python,Random,Passwords,Generator,Change Password,我正在用字典构建自己的密码生成器,并检查里面是否有每种类型的字符。它工作正常,但我想我把支票编码得有点复杂 如果有更好的编码方法,你有什么想法吗。 有没有办法从检查中解脱出来,如果它在下降中已经准备好了,那么它就不会检查其他类型了 注:我想自己定义一下ussed lowers/uppers/specials/NUM,这样我就可以避免添加我不喜欢的chars chars = "" alpha_lowers = "abcdefghijklmnopqrstuvwxyz&
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的操作。例如,您可以通过以下方式检查密码是否包含小写字母:
alpha_lowers = "abcdefghijklmnopqrstuvwxyz"
password = "password"
haslower = bool(set(password).intersection(alpha_lowers))
print(haslower) # True
说明:我确实将字母集设置为alpha_lowers和password的公共字母集,然后将其转换为bool,如果该集为空,则结果为False,否则为True
有没有办法从检查中解脱出来,如果它在下降中已经准备好了,那么它就不会检查其他类型了
由于您已经有了函数,您可能会在检查未通过后立即返回invalid。它工作正常,但我认为我对检查的编码有点复杂。那么这不是合适的地方。你正在寻找这个:也许在代码审查SE中发布??在那里,社区成员会审查和改进您的代码。您的检查是拒绝抽样的一种形式;您最好只从所需的类中拾取字符,然后用所有类的统一绘制填充剩余长度。最后一次洗牌可以用来将所需的类放在随机位置。谢谢操作,我来看看!对于你提到的操作,我认为它应该与返回一起工作;