comparison cutils/genpwd.py @ 243:86417af99561

genpwd: Implement ascii and alnum and their safe variants
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Feb 2025 10:39:49 +0100
parents ae9bc3006efc
children 42f7ecd70ec1
comparison
equal deleted inserted replaced
242:ae9bc3006efc 243:86417af99561
25 import sys 25 import sys
26 26
27 from . import (__version__, __revision__) 27 from . import (__version__, __revision__)
28 28
29 29
30 WEB_CHARS = b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz" \ 30 WEB_CHARS = (b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz"
31 b"0123456789.,-_;!()[]{}*" 31 b"0123456789.,-_;!()[]{}*")
32 WEB_SAFE_CHARS = b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz" \ 32 WEB_SAFE_CHARS = (b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz"
33 b"23456789.,-_;!" 33 b"23456789.,-_;!")
34 WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS # prefer punctionation chars 34 WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS # prefer punctionation chars
35 # Most visible characters but no space
36 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~")
38 #
39 # A safer variant of FULL_ASCII:
40 # - no characters that are visually similar (0O, 1lI)
41 # - no characters with dead keys on german keyboards
42 # - no backslash (too easily interpret as escape character
43 # - no single or double quotes
44 SAFE_ASCII = (b"23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
45 b"abcdefghijkmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@_[]{|}~")
46 # just numeric and alphabetic
47 ALNUM = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
48 # safer alpha-numberic without visually similar characters
49 SAFE_ALNUM = b"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
50
35 51
36 PY2 = sys.version_info[0] <= 2 52 PY2 = sys.version_info[0] <= 2
37 53
38 54
39 def main(argv=None): 55 def main(argv=None):
48 "-E", dest="use_bin_length", action="store_true", 64 "-E", dest="use_bin_length", action="store_true",
49 help="For some repertoires make OUTPUT-LENGTH the number of bytes" 65 help="For some repertoires make OUTPUT-LENGTH the number of bytes"
50 " that is to be read from random sources instead of output bytes") 66 " that is to be read from random sources instead of output bytes")
51 aparser.add_argument( 67 aparser.add_argument(
52 "--repertoire", "--type", "-t", 68 "--repertoire", "--type", "-t",
53 choices=("web", "web-safe", "web-safe2", 69 choices=("web", "web-safe", "web-safe2", "ascii", "safe-ascii",
70 "alnum", "safe-alnum",
54 "bin-base64", "bin-urlsafe-base64", "bin-base32", 71 "bin-base64", "bin-urlsafe-base64", "bin-base32",
55 "bin-ascii85", "bin-hex", ), 72 "bin-ascii85", "bin-hex", ),
56 default="web-safe2", 73 default="web-safe2",
57 help=""" 74 help="""
58 Select from a character repertoire. 75 Select from a character repertoire.
70 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS) 87 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS)
71 elif opts.repertoire == "web-safe": 88 elif opts.repertoire == "web-safe":
72 pwd = gen_from_repertoire(opts.req_length, WEB_SAFE_CHARS) 89 pwd = gen_from_repertoire(opts.req_length, WEB_SAFE_CHARS)
73 elif opts.repertoire == "web-safe2": 90 elif opts.repertoire == "web-safe2":
74 pwd = gen_from_repertoire(opts.req_length, WEB_SAFE2_CHARS) 91 pwd = gen_from_repertoire(opts.req_length, WEB_SAFE2_CHARS)
92 elif opts.repertoire == "ascii":
93 pwd = gen_from_repertoire(opts.req_length, FULL_ASCII)
94 elif opts.repertoire == "safe-ascii":
95 pwd = gen_from_repertoire(opts.req_length, SAFE_ASCII)
96 elif opts.repertoire == "alnum":
97 pwd = gen_from_repertoire(opts.req_length, ALNUM)
98 elif opts.repertoire == "safe-alnum":
99 pwd = gen_from_repertoire(opts.req_length, SAFE_ALNUM)
75 elif opts.repertoire == "bin-base64": 100 elif opts.repertoire == "bin-base64":
76 encoder = base64.b64encode 101 encoder = base64.b64encode
77 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder, 102 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
78 rstrip_chars=b"=") 103 rstrip_chars=b"=")
79 elif opts.repertoire == "bin-urlsafe-base64": 104 elif opts.repertoire == "bin-urlsafe-base64":