comparison cutils/genpwd.py @ 246:c29266444003

genpwd: Implement "uri"
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Feb 2025 11:43:33 +0100
parents 35c06dcca856
children 435687f4e071
comparison
equal deleted inserted replaced
245:35c06dcca856 246:c29266444003
31 # Unreserved characters according to RFC 1738 (URL) **and** RFC 3986 (URI) 31 # Unreserved characters according to RFC 1738 (URL) **and** RFC 3986 (URI)
32 # No general delimiters and no sub-delimiters. 32 # No general delimiters and no sub-delimiters.
33 # 33 #
34 WEB_CHARS = (b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz" 34 WEB_CHARS = (b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz"
35 b"0123456789-._") 35 b"0123456789-._")
36 # WEB_CHARS without visually similar characters (0O, 1lI)
36 SAFE_WEB_CHARS = (b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz" 37 SAFE_WEB_CHARS = (b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz"
37 b"23456789-._") 38 b"23456789-._")
38 SAFE_WEB_CHARS_2 = b".-_" + SAFE_WEB_CHARS # prefer punctionation chars 39 # SAFE_WEB_CHARS with preference to punctuation
39 # Most visible characters but no space 40 SAFE_WEB_CHARS_2 = b".-_" + SAFE_WEB_CHARS
41 # Unreserved characters from URI but with sub-delims allowed
42 URI_CHARS = WEB_CHARS + b"~" + b"!$&'()*+,;="
43 # URI_CHARS without visually similar characters
44 SAFE_URI_CHARS = SAFE_WEB_CHARS + b"~" + b"!$&'()*+,;="
45 # Just like SAFE_URI_CHARS but prefers punctuation characters
46 SAFE_URI_CHARS_2 = (b"~" + b"!$&'()*+,;=" + SAFE_WEB_CHARS
47 + b"~" + b"!$&'()*+,;=")
48 # All visible characters from ASCII character set but no space
40 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 49 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~") 50 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~")
42 # 51 #
43 # A safer variant of FULL_ASCII: 52 # A safer variant of FULL_ASCII:
44 # - no characters that are visually similar (0O, 1lI) 53 # - no characters that are visually similar (0O, 1lI)
68 "-E", dest="use_bin_length", action="store_true", 77 "-E", dest="use_bin_length", action="store_true",
69 help="For some repertoires make OUTPUT-LENGTH the number of bytes" 78 help="For some repertoires make OUTPUT-LENGTH the number of bytes"
70 " that is to be read from random sources instead of output bytes") 79 " that is to be read from random sources instead of output bytes")
71 aparser.add_argument( 80 aparser.add_argument(
72 "--repertoire", "--type", "-t", 81 "--repertoire", "--type", "-t",
73 choices=("web", "safe-web", "safe-web-2", "ascii", "safe-ascii", 82 choices=("web", "safe-web", "safe-web-2",
83 "uri", "safe-uri", "safe-uri-2",
84 "ascii", "safe-ascii",
74 "alnum", "safe-alnum", 85 "alnum", "safe-alnum",
75 "bin-base64", "bin-urlsafe-base64", "bin-base32", 86 "bin-base64", "bin-urlsafe-base64", "bin-base32", "bin-hex",
76 "bin-ascii85", "bin-hex", ), 87 "bin-ascii85",),
77 default="safe-ascii", 88 default="safe-ascii",
78 help=""" 89 help="""
79 Select from a character repertoire. 90 Select from a character repertoire.
80 All repertoires that start with "bin-" just encode the output of 91 All repertoires that start with "bin-" just encode the output of
81 "os.urandom()" with the selected encoder. 92 "os.urandom()" with the selected encoder.
91 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS) 102 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS)
92 elif opts.repertoire == "safe-web": 103 elif opts.repertoire == "safe-web":
93 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS) 104 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS)
94 elif opts.repertoire == "safe-web-2": 105 elif opts.repertoire == "safe-web-2":
95 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS_2) 106 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS_2)
107 elif opts.repertoire == "uri":
108 pwd = gen_from_repertoire(opts.req_length, URI_CHARS)
109 elif opts.repertoire == "safe-uri":
110 pwd = gen_from_repertoire(opts.req_length, SAFE_URI_CHARS)
111 elif opts.repertoire == "safe-uri-2":
112 pwd = gen_from_repertoire(opts.req_length, SAFE_URI_CHARS_2)
96 elif opts.repertoire == "ascii": 113 elif opts.repertoire == "ascii":
97 pwd = gen_from_repertoire(opts.req_length, FULL_ASCII) 114 pwd = gen_from_repertoire(opts.req_length, FULL_ASCII)
98 elif opts.repertoire == "safe-ascii": 115 elif opts.repertoire == "safe-ascii":
99 pwd = gen_from_repertoire(opts.req_length, SAFE_ASCII) 116 pwd = gen_from_repertoire(opts.req_length, SAFE_ASCII)
100 elif opts.repertoire == "alnum": 117 elif opts.repertoire == "alnum":