Last active
May 6, 2019 16:12
-
-
Save jsocol/8269951728473d03aa6e310c0f233a1b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
from __future__ import absolute_import, print_function, unicode_literals | |
import argparse | |
import random | |
import re | |
import string | |
sr = random.SystemRandom() | |
TYPES = { | |
'all': re.sub(r'\s+', '', string.printable), | |
'az': string.ascii_letters, | |
'num': string.digits, | |
'number': string.digits, | |
'w': string.ascii_letters + string.digits, | |
} | |
def make_pw(chars, length): | |
return ''.join([sr.choice(chars) for _ in range(length)]) | |
def get_parser(): | |
parser = argparse.ArgumentParser( | |
description='Generate a secure random password', | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
) | |
parser.add_argument('-l', '--length', type=int, default=30, | |
help='Length of the generated password') | |
parser.add_argument('-n', '--numeric', action='store_true', default=False, | |
help='Only use numbers (same as `-c num`)') | |
parser.add_argument('-c', '--chars', choices=TYPES.keys(), default='all', | |
help='Allowed character types') | |
return parser | |
def main(argv=None): | |
parser = get_parser() | |
options = parser.parse_args(argv) | |
if options.numeric: | |
chars = TYPES['num'] | |
else: | |
chars = TYPES.get(options.chars) | |
print(make_pw(chars, options.length)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment