Last active
May 26, 2021 14:59
-
-
Save ankel/e8d3580bb5a1d848f1c375ef666d2c28 to your computer and use it in GitHub Desktop.
Generate password with configurable included character classes.
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
#!/usr/bin/env python3 | |
import argparse | |
import secrets | |
import string | |
def GetParser(): | |
parser = argparse.ArgumentParser(description="A very opinionated password generator.") | |
parser.add_argument( | |
'-a', '--lower', action='store_const', const=True, help='include lower case alphabets, default is true.') | |
parser.add_argument( | |
'-A', '--upper', action='store_const', const=True, help='include upper case alphabets. default is true.') | |
parser.add_argument( | |
'-1', '--number', action='store_const', const=True, help='include numbers. default is true.') | |
parser.add_argument( | |
'-@', '--special', action='store_const', const=True, help='include special characters. default is false.') | |
parser.add_argument( | |
'length', action='store', default=6, nargs='?', help='the length of the password, default 6') | |
parser.add_argument( | |
'count', action='store', default=5, nargs='?', help='the number of passwords to be generated, default 5') | |
return parser | |
def generate_charset(lower, upper, number, special): | |
ret = [] | |
if lower: | |
ret += list(string.ascii_lowercase) | |
if upper: | |
ret += list(string.ascii_uppercase) | |
if number: | |
ret += list(string.digits) | |
if special: | |
ret += list("!@#$%^&*_+=-") | |
return ret | |
def main(): | |
args = GetParser().parse_args() | |
chars = [] | |
if not args.lower and not args.upper and not args.number: | |
chars = generate_charset(True, True, True, args.special) | |
else: | |
chars = generate_charset( | |
args.lower, args.upper, args.number, args.special) | |
for i in range(args.count): | |
pw = ''.join(secrets.choice(chars) for i in range(args.length)) | |
print(pw) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment