简体   繁体   English

随机密码不会选择用户选择的每个条件

[英]random password does not choose every condition that user choose

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
    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(length))

    return password

a = random_password_generator()
print(a)

secrets.choice(phrase) chooses a random character out of the phrase you give it. secrets.choice(phrase) 从你给它的短语中选择一个随机字符。 You perform that operation 10 times.您执行该操作 10 次。 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.无法保证在这 10 次中,随机选择将属于您添加到短语中的任何类别。

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

You're choosing ten random letters from phrase .您正在从phrase中选择十个随机字母。

Even if phrase includes uppercase letters as possible choices, there's no guarantee that the random selections will actually include any uppercase letters.即使phrase包含大写字母作为可能的选择,也不能保证随机选择实际上包含任何大写字母。 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.您忽略了length参数。

Take the input length and divide it into symbol categories.取输入长度并将其划分为符号类别。 Randomly select characters from that category and append them to the password.从该类别中随机 select 字符和 append 到密码。 Finally, randomly shuffle all characters of the password.最后,随机打乱密码的所有字符。

Dividing the input length into categories could be done according to some weights that you choose and hard-code.可以根据您选择的一些权重和硬编码来将输入长度划分为类别。

For example, if you were to assign the following weights,例如,如果您要分配以下权重,

Category类别 weight重量
Upper 5 5
Lower降低 5 5
Digits数字 3 3
Symbols符号 2 2

Then, you can decide how many characters to use for each category.然后,您可以决定每个类别使用多少个字符。 For example, if they ask for a length of 12, with upper, lower, and digits.例如,如果他们要求长度为 12,包括大写、小写和数字。 The total weight for upper, lower, and digits is 5 + 5 + 3 = 13, therefore max(( 5 / 13 ) * 12, 1) = 4 characters should be upper case, etc.大写、小写和数字的总权重为 5 + 5 + 3 = 13,因此 max(( 5 / 13 ) * 12, 1) = 4 个字符应为大写等。

Your function argument to decide wether symbols should be included is called symbols .您决定是否应包含符号的 function 参数称为symbols However, you then assign string.punctuation to a variable with the same name.但是,您随后将string.punctuation分配给具有相同名称的变量。

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.这意味着,以下 if 语句if symbol:实际上与if string.punctuation相同,它始终解析为True ,无论您的 symbol 参数在 string.punctuation 分配给它之前携带的 boolean 值是什么。

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.我希望这是可以理解的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM