Home > Software design >  random password does not choose every condition that user choose
random password does not choose every condition that user choose

Time:07-28

I want to make a password generator I let user to choose upper, digits and, symbols but I have problem with Random part when my code choose a random password from phrase According to the conditions selected by the user, one of the conditions may not be fulfilled. For example, all the selected phrase may be lowercase, but the user has activated the uppercase condition

import  string
import secrets


def random_password_generator(upper=True, digits=True, symbols=False, length=12):
    a_l = string.ascii_lowercase
    a_u = string.ascii_uppercase
    symbols = string.punctuation
    phrase = a_l
    if upper:
        phrase  = a_u

    if digits:
        phrase  = string.digits

    if symbols:
        phrase  = string.punctuation

    password = ''.join(secrets.choice(phrase) for i in range(10))

    return password

a = random_password_generator()
print(a)

CodePudding user response:

secrets.choice(phrase) chooses a random character out of the phrase you give it. You perform that operation 10 times. There is no guarantee that during those 10 times, the random choice will fall in any of the categories you have added to your phrase.

CodePudding user response:

password = ''.join(secrets.choice(phrase) for i in range(10))

You're choosing ten random letters from phrase.

Even if phrase includes uppercase letters as possible choices, there's no guarantee that the random selections will actually include any uppercase letters. It might just happen to randomly choose all lowercase letters.

Same thing with digits and symbols.

Also, why are you choosing ten letters? You're ignoring the length parameter.

CodePudding user response:

Your function argument to decide wether symbols should be included is called symbols. However, you then assign string.punctuation to a variable with the same name.

This means, the following if statement if symbol: is actually the same as if string.punctuation, which always resolves to True, no matter what boolean value your symbol parameter carried before string.punctuation was assigned to it.

Here is a fixed version of your code:

import  string
import secrets


def random_password_generator(upper=True, digits=True, include_symbols=False, length=12):
    a_l = string.ascii_lowercase
    a_u = string.ascii_uppercase
    all_symbols = string.punctuation # make sure this is a different variable
    phrase = a_l
    if upper:
        phrase  = a_u

    if digits:
        phrase  = string.digits

    if include_symbols:
        phrase  = all_symbols

    password = ''.join(secrets.choice(phrase) for i in range(10))

    return password

a = random_password_generator()
print(a)

I hope this was understandable.

  • Related