Created
June 21, 2021 14:02
-
-
Save yoniLavi/4a5f9e935c92748790113dc027e66121 to your computer and use it in GitHub Desktop.
Example function to generate a random password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from random import choice, shuffle | |
| from string import ascii_lowercase, ascii_uppercase, digits | |
| def generate_password(length=16, groups=[ascii_lowercase, ascii_uppercase, digits]): | |
| """Generate a random strong password. | |
| It will be of length `length`, with at least one character of each of the `groups`. | |
| """ | |
| group_indices = list(range(len(groups))) | |
| chosen_indices = [choice(group_indices) for _ in range(length - len(groups))] | |
| chosen_indices.extend(group_indices) # at least one of each | |
| shuffle(chosen_indices) # need to shuffle because of the above's fixed position | |
| return ''.join(choice(groups[index]) for index in chosen_indices) | |
| if __name__ == '__main__': | |
| print(f'Your new generated password is: {generate_password()}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment