Skip to content

Instantly share code, notes, and snippets.

@jackylamhk
Last active November 7, 2023 12:47
Show Gist options
  • Save jackylamhk/d9d7f5f8d0438c9a05f7f2ed523df1ec to your computer and use it in GitHub Desktop.
Save jackylamhk/d9d7f5f8d0438c9a05f7f2ed523df1ec to your computer and use it in GitHub Desktop.
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