Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/genpwd.py @ 231:6d8443878a00
Use argparse in genpwd.py now.
As side effect there is now a help and version flag.
And the help message are slightly better.
Also added a genpwd.py driver program.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 07 Feb 2025 13:30:33 +0100 |
| parents | ccbb6905914e |
| children | 7ac8a2537bc9 |
comparison
equal
deleted
inserted
replaced
| 230:ccbb6905914e | 231:6d8443878a00 |
|---|---|
| 16 | 16 |
| 17 """ | 17 """ |
| 18 | 18 |
| 19 from __future__ import (division, absolute_import, print_function) | 19 from __future__ import (division, absolute_import, print_function) |
| 20 | 20 |
| 21 import getopt | 21 import argparse |
| 22 import base64 | |
| 23 import os | |
| 22 import sys | 24 import sys |
| 23 import os | |
| 24 import base64 | |
| 25 | 25 |
| 26 from . import (__version__, __revision__) | 26 from . import (__version__, __revision__) |
| 27 | 27 |
| 28 | 28 |
| 29 WEB_CHARS = b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz0123456789.,-_;!()[]{}*" | 29 WEB_CHARS = b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz" \ |
| 30 WEB_SAFE_CHARS = b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz23456789.,-_;!" | 30 b"0123456789.,-_;!()[]{}*" |
| 31 WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS | 31 WEB_SAFE_CHARS = b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz" \ |
| 32 b"23456789.,-_;!" | |
| 33 WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS # prefer punctionation chars | |
| 32 | 34 |
| 33 PY2 = sys.version_info[0] <= 2 | 35 PY2 = sys.version_info[0] <= 2 |
| 34 | 36 |
| 35 | 37 |
| 36 def main(): | 38 def main(argv=None): |
| 37 opt_type = "web-safe2" | 39 aparser = argparse.ArgumentParser( |
| 38 opts, args = getopt.getopt(sys.argv[1:], | 40 description="A simple password generator for password of a given" |
| 39 "t:", | 41 " length within a character repertoire", |
| 40 ["type="]) | 42 fromfile_prefix_chars='@') |
| 41 for opt, val in opts: | 43 aparser.add_argument( |
| 42 if opt in ("-t", "--type"): | 44 "--repertoire", "--type", "-t", |
| 43 if val not in ("web", "web-safe", "web-safe2", | 45 choices=("web", "web-safe", "web-safe2", |
| 44 "base64", "base32", "ascii85", ): | 46 "base64", "base32", "ascii85", ), |
| 45 raise getopt.GetoptError("invalid type: %s" % val, "") | 47 default="web-safe2", |
| 46 opt_type = val | 48 help="Select the character repertoire. Default: web-safe2") |
| 47 else: | 49 aparser.add_argument( |
| 48 raise RuntimeError("inconsistent getopt handling") | 50 "--version", "-v", action="version", |
| 51 version="%s (rv:%s)" % (__version__, __revision__)) | |
| 52 aparser.add_argument( | |
| 53 "req_length", metavar="OUTPUT-LENGTH", type=int, | |
| 54 help="The required length of the generated password") | |
| 49 | 55 |
| 50 try: | 56 opts = aparser.parse_args(args=argv) |
| 51 req_length = int(args[0], 10) | |
| 52 except IndexError: | |
| 53 raise getopt.GetoptError("no length given") | |
| 54 | 57 |
| 55 if opt_type == "web": | 58 if opts.repertoire == "web": |
| 56 pwd = gen_web(req_length, WEB_CHARS) | 59 pwd = gen_web(opts.req_length, WEB_CHARS) |
| 57 elif opt_type == "web-safe": | 60 elif opts.repertoire == "web-safe": |
| 58 pwd = gen_web(req_length, WEB_SAFE_CHARS) | 61 pwd = gen_web(opts.req_length, WEB_SAFE_CHARS) |
| 59 elif opt_type == "web-safe2": | 62 elif opts.repertoire == "web-safe2": |
| 60 pwd = gen_web(req_length, WEB_SAFE2_CHARS) | 63 pwd = gen_web(opts.req_length, WEB_SAFE2_CHARS) |
| 61 elif opt_type == "base64": | 64 elif opts.repertoire == "base64": |
| 62 encoder = base64.b64encode | 65 encoder = base64.b64encode |
| 63 pwd = gen_bin(req_length, encoder) | 66 pwd = gen_bin(opts.req_length, encoder) |
| 64 elif opt_type == "base32": | 67 elif opts.repertoire == "base32": |
| 65 encoder = base64.b32encode | 68 encoder = base64.b32encode |
| 66 pwd = gen_bin(req_length, encoder) | 69 pwd = gen_bin(opts.req_length, encoder) |
| 67 elif opt_type == "ascii85": | 70 elif opts.repertoire == "ascii85": |
| 68 encoder = base64.a85encode | 71 encoder = base64.a85encode |
| 69 pwd = gen_bin(req_length, encoder) | 72 pwd = gen_bin(opts.req_length, encoder) |
| 70 else: | 73 else: |
| 71 raise NotImplementedError("type not yet implemented: %s" % opt_type) | 74 raise NotImplementedError("type not yet implemented: %s" |
| 75 % opts.repertoire) | |
| 72 print(pwd) | 76 print(pwd) |
| 73 | 77 |
| 74 | 78 |
| 75 def gen_web(length, chars): | 79 def gen_web(length, chars): |
| 76 mult = 256//len(chars) | 80 mult = 256//len(chars) |
