Created
April 13, 2024 20:09
-
-
Save evan-burke/a6b165c08d06b3e2be5e290ab7dc589a 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
# simple password generator | |
# https://docs.python.org/3/library/secrets.html#recipes-and-best-practices | |
import argparse | |
import secrets | |
import string | |
# todo: add args for specialchars etc | |
# specify min numbers of capital letters, digits, etc. | |
digits = list(string.digits) | |
alphabets = list(string.ascii_letters) | |
specialchars = list("!@#$%^&*()") | |
def generatePassword(length, allow_digits, allow_alphabets, allow_specialchars): | |
length = int(length) | |
if length <= 4: | |
print("too short, length should be at least 4 chars") | |
exit() | |
elif length >= 70: | |
print("too long, use length less than 70") | |
chars = [] | |
if allow_digits == True: | |
chars += digits | |
if allow_alphabets == True: | |
chars += alphabets | |
if allow_specialchars == True: | |
chars += specialchars | |
return ''.join(secrets.choice(chars) for i in range(length)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='generate a random password') | |
parser.add_argument('-l', '-length', '--length', type=int, nargs="?", default=20) | |
args = parser.parse_args() | |
p = generatePassword(length=args.length, allow_digits=True, allow_alphabets=True, allow_specialchars=False) | |
print(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment