Last active
November 7, 2023 12:47
-
-
Save jackylamhk/d9d7f5f8d0438c9a05f7f2ed523df1ec to your computer and use it in GitHub Desktop.
This file contains 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
import secrets | |
import random | |
import string | |
# Source: https://www.geeksforgeeks.org/generating-strong-password-using-python/ | |
def generate_password(length: int, use_symbols: bool = False): | |
ALL_CANDIDATES = string.ascii_letters + string.digits | |
password = [ | |
secrets.choice(string.digits), | |
secrets.choice(string.ascii_lowercase), | |
secrets.choice(string.ascii_uppercase), | |
] | |
if use_symbols: | |
SYMBOLS = "@#$%=:?./|~>*()<" | |
ALL_CANDIDATES += SYMBOLS | |
password.append(secrets.choice(SYMBOLS)) | |
for _ in range(length - len(password)): | |
password += secrets.choice(ALL_CANDIDATES) | |
# Prevent the generated password from having a consistent pattern | |
random.shuffle(password) | |
return "".join(password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment