comparison cutils/genpwd.py @ 249:f161448d673e

genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Feb 2025 13:26:39 +0100
parents 4796e5da04ee
children 48f89d312309
comparison
equal deleted inserted replaced
248:4796e5da04ee 249:f161448d673e
71 " length within a character repertoire", 71 " length within a character repertoire",
72 fromfile_prefix_chars='@') 72 fromfile_prefix_chars='@')
73 aparser.add_argument( 73 aparser.add_argument(
74 "--version", "-v", action="version", 74 "--version", "-v", action="version",
75 version="%s (rv:%s)" % (__version__, __revision__)) 75 version="%s (rv:%s)" % (__version__, __revision__))
76 aparser.add_argument( 76 group = aparser.add_mutually_exclusive_group()
77 group.add_argument(
77 "--algorithm", "-a", 78 "--algorithm", "-a",
78 choices=("web", "safe-web", "safe-web-2", 79 choices=("web", "safe-web", "safe-web-2",
79 "uri", "safe-uri", "safe-uri-2", 80 "uri", "safe-uri", "safe-uri-2",
80 "ascii", "safe-ascii", 81 "ascii", "safe-ascii",
81 "alnum", "safe-alnum", 82 "alnum", "safe-alnum",
87 All repertoires that start with `bin-' just encode the output of 88 All repertoires that start with `bin-' just encode the output of
88 "os.urandom()" with the selected encoder. 89 "os.urandom()" with the selected encoder.
89 All repertoires that end with `-safe' or `safe-2' do not contain visually 90 All repertoires that end with `-safe' or `safe-2' do not contain visually
90 similar characters (currently `0O' or `Il1'). 91 similar characters (currently `0O' or `Il1').
91 All repertoires that end with `-2' are variants with a bias to punctuation 92 All repertoires that end with `-2' are variants with a bias to punctuation
92 characters. 93 characters.
93 Default: safe-ascii 94 This is incompatible with option `--repertoire'.
94 """) 95 Default: safe-ascii""")
96 group.add_argument(
97 "--repertoire", "-r",
98 action="store", metavar="REPERTOIRE",
99 help="""
100 Select from given character repertoire.
101 The repertoire must be characters from the ISO-8859-15 character set.
102 An empty REPERTOIRE selects implicitly the default algorithm.
103 This is incompatible with option `--algorithm'.""")
95 aparser.add_argument( 104 aparser.add_argument(
96 "-E", dest="use_bin_length", action="store_true", 105 "-E", dest="use_bin_length", action="store_true",
97 help="For some repertoires make OUTPUT-LENGTH the number of bytes" 106 help="For some repertoires make OUTPUT-LENGTH the number of bytes"
98 " that is to be read from random sources instead of output bytes") 107 " that is to be read from random sources instead of output bytes")
99 aparser.add_argument( 108 aparser.add_argument(
100 "req_length", metavar="OUTPUT-LENGTH", type=int, 109 "req_length", metavar="OUTPUT-LENGTH", type=int,
101 help="The required length of the generated output") 110 help="The required length of the generated output")
102 111
103 opts = aparser.parse_args(args=argv) 112 opts = aparser.parse_args(args=argv)
104 113
105 if opts.algorithm == "web": 114 if opts.repertoire:
115 try:
116 repertoire = (opts.repertoire
117 if PY2
118 else opts.repertoire.encode("iso-8859-15"))
119 except UnicodeError:
120 raise ValueError("non ISO-8859-15 character in given repertoire")
121 pwd = gen_from_repertoire(opts.req_length, repertoire)
122 elif opts.algorithm == "web":
106 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS) 123 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS)
107 elif opts.algorithm == "safe-web": 124 elif opts.algorithm == "safe-web":
108 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS) 125 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS)
109 elif opts.algorithm == "safe-web-2": 126 elif opts.algorithm == "safe-web-2":
110 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS_2) 127 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS_2)
148 raise AssertionError("internal length mismatch") 165 raise AssertionError("internal length mismatch")
149 else: 166 else:
150 if len(pwd) != opts.req_length: 167 if len(pwd) != opts.req_length:
151 raise AssertionError("internal length mismatch") 168 raise AssertionError("internal length mismatch")
152 if not PY2: 169 if not PY2:
153 pwd = pwd.decode("ascii") 170 pwd = pwd.decode("iso-8859-15")
154 print(pwd) 171 print(pwd)
155 172
156 173
157 def gen_from_repertoire(length, repertoire): 174 def gen_from_repertoire(length, repertoire):
158 """Select `length` characters randomly from given character repertoire 175 """Select `length` characters randomly from given character repertoire