Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when length less than 4 in Provider().password() #2176

Open
jianda1013 opened this issue Feb 11, 2025 · 1 comment
Open

Error when length less than 4 in Provider().password() #2176

jianda1013 opened this issue Feb 11, 2025 · 1 comment

Comments

@jianda1013
Copy link

I got AssertionError: Required length is shorter than required characters when using Faker().password(length = 2)

the error occurs when the input length is less than the character pool.

here is the current code
https://github.com/joke2k/faker/blob/master/faker/providers/misc/__init__.py#L144

        choices = ""
        required_tokens = []
        if special_chars:
            required_tokens.append(self.generator.random.choice("!@#$%^&*()_+"))
            choices += "!@#$%^&*()_+"
        if digits:
            required_tokens.append(self.generator.random.choice(string.digits))
            choices += string.digits
        if upper_case:
            required_tokens.append(self.generator.random.choice(string.ascii_uppercase))
            choices += string.ascii_uppercase
        if lower_case:
            required_tokens.append(self.generator.random.choice(string.ascii_lowercase))
            choices += string.ascii_lowercase
        assert len(required_tokens) <= length, "Required length is shorter than required characters"

maybe need to separate the required token and the available token for the input?

@DevFoxxx
Copy link

To resolve the error, you can automatically increase the password length if it is less than the number of required categories, setting it at least equal to the number of required categories. Subsequently, the characters are completed with random ones from the available pool, still respecting the categories, and finally they are mixed to avoid predictable patterns. For example:

# If the required tokens exceed the requested length, adjust the length to be at least the number of required categories
if len(required_tokens) > length:
    length = len(required_tokens)

# Create the password starting with the mandatory required tokens
chars = required_tokens

# Add random characters from the available pool until the password reaches the requested length
while len(chars) < length:
    chars.append(self.generator.random.choice(available_tokens))

# Shuffle the characters to avoid predictable patterns and ensure randomness
random.shuffle(chars)


# Return the password as a string, truncated to the desired length
return ''.join(chars[:length])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants