Password-Generator Valid-Password Check

79 Views Asked by At

I'm building my own Password Generator with Dictionary and Check if there is a char from every type inside. It works fine but i think i coded the check a little bit complicated.

Do you have ideas if there is a way to code this a better way. And is there a way to break free from the check if it is allready in the lowers so it dont checks the other types?

PS: i want wo define the ussed lowers/uppers/specials/nums my self so i can allways avoid chars getting added i dont like.


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")

1

There are 1 best solutions below

1
On

Do you have ideas if there is a way to code this a better way.

I suggest taking look at set's operation intersect. For example you can check if password contain lowercase letter following way:

alpha_lowers = "abcdefghijklmnopqrstuvwxyz"
password = "password"
haslower = bool(set(password).intersection(alpha_lowers))
print(haslower)  # True

Explanation: I do make set of letters common both to alpha_lowers and password, then convert it to bool, which result in False if that set is empty and True otherwise.

And is there a way to break free from the check if it is allready in the lowers so it dont checks the other types?

As you already have function you might just return "invalid" immediately after check was not pass.