annotate cutils/genpwd.py @ 244:42f7ecd70ec1

genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Feb 2025 10:52:02 +0100
parents 86417af99561
children 35c06dcca856
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
230
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
2 # :-
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
3 # :Copyright: (c) 2018 Franz Glasner
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
4 # :Copyright: (c) 2025 Franz Glasner
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
5 # :License: BSD-3-Clause
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
6 # :-
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
7 r"""Generate passwords.
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
8
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
9 Usage: genpwd.py [ Options ] required_length
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
10
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
11 Options:
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
12
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
13 --type, -t web, web-safe, web-safe2, base64, base32, ascii85
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
14
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
15 :Author: Franz Glasner
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
16
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
17 """
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
18
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
19 from __future__ import (division, absolute_import, print_function)
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
20
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
21 import argparse
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
22 import base64
241
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
23 import binascii
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
24 import os
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
25 import sys
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
26
230
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
27 from . import (__version__, __revision__)
ccbb6905914e Change copyright and note genpwd in the READNE and make an official script
Franz Glasner <fzglas.hg@dom66.de>
parents: 229
diff changeset
28
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
29
244
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
30 #
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
31 # Unreserved characters according to RFC 1738 (URL) **and** RFC 3986 (URI)
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
32 # No general delimiters and no sub-delimiters.
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
33 #
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
34 WEB_CHARS = (b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz"
244
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
35 b"0123456789-._")
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
36 SAFE_WEB_CHARS = (b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz"
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
37 b"23456789-._")
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
38 SAFE_WEB_CHARS_2 = b".-_" + SAFE_WEB_CHARS # prefer punctionation chars
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
39 # Most visible characters but no space
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
40 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
41 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~")
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
42 #
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
43 # A safer variant of FULL_ASCII:
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
44 # - no characters that are visually similar (0O, 1lI)
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
45 # - no characters with dead keys on german keyboards
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
46 # - no backslash (too easily interpret as escape character
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
47 # - no single or double quotes
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
48 SAFE_ASCII = (b"23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
49 b"abcdefghijkmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@_[]{|}~")
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
50 # just numeric and alphabetic
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
51 ALNUM = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
52 # safer alpha-numberic without visually similar characters
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
53 SAFE_ALNUM = b"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
54
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
55
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
56 PY2 = sys.version_info[0] <= 2
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
57
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
58
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
59 def main(argv=None):
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
60 aparser = argparse.ArgumentParser(
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
61 description="A simple password generator for password of a given"
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
62 " length within a character repertoire",
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
63 fromfile_prefix_chars='@')
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
64 aparser.add_argument(
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
65 "--version", "-v", action="version",
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
66 version="%s (rv:%s)" % (__version__, __revision__))
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
67 aparser.add_argument(
236
939a8da6bc92 genpwd: change the current "-e" option to "-E"
Franz Glasner <fzglas.hg@dom66.de>
parents: 235
diff changeset
68 "-E", dest="use_bin_length", action="store_true",
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
69 help="For some repertoires make OUTPUT-LENGTH the number of bytes"
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
70 " that is to be read from random sources instead of output bytes")
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
71 aparser.add_argument(
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
72 "--repertoire", "--type", "-t",
244
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
73 choices=("web", "safe-web", "safe-web-2", "ascii", "safe-ascii",
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
74 "alnum", "safe-alnum",
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
75 "bin-base64", "bin-urlsafe-base64", "bin-base32",
241
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
76 "bin-ascii85", "bin-hex", ),
244
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
77 default="safe-web-2",
242
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
78 help="""
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
79 Select from a character repertoire.
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
80 All repertoires that start with "bin-" just encode the output of
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
81 "os.urandom()" with the selected encoder.
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
82 Default: web-safe2
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
83 """)
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
84 aparser.add_argument(
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
85 "req_length", metavar="OUTPUT-LENGTH", type=int,
237
d65a4d861dad Enhance help message
Franz Glasner <fzglas.hg@dom66.de>
parents: 236
diff changeset
86 help="The required length of the generated output")
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
87
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
88 opts = aparser.parse_args(args=argv)
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
89
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
90 if opts.repertoire == "web":
239
1eae57292c7c genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 238
diff changeset
91 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS)
244
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
92 elif opts.repertoire == "safe-web":
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
93 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS)
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
94 elif opts.repertoire == "safe-web-2":
42f7ecd70ec1 genpwd: Renamed algorithms and changed restricted the WEB character repertoire to not use delims and sub-delims from URLs and URIs
Franz Glasner <fzglas.hg@dom66.de>
parents: 243
diff changeset
95 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS_2)
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
96 elif opts.repertoire == "ascii":
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
97 pwd = gen_from_repertoire(opts.req_length, FULL_ASCII)
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
98 elif opts.repertoire == "safe-ascii":
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
99 pwd = gen_from_repertoire(opts.req_length, SAFE_ASCII)
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
100 elif opts.repertoire == "alnum":
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
101 pwd = gen_from_repertoire(opts.req_length, ALNUM)
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
102 elif opts.repertoire == "safe-alnum":
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
103 pwd = gen_from_repertoire(opts.req_length, SAFE_ALNUM)
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
104 elif opts.repertoire == "bin-base64":
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
105 encoder = base64.b64encode
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
106 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
107 rstrip_chars=b"=")
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
108 elif opts.repertoire == "bin-urlsafe-base64":
232
7ac8a2537bc9 Implement the urlsafe base64 character repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 231
diff changeset
109 encoder = base64.urlsafe_b64encode
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
110 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
111 rstrip_chars=b"=")
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
112 elif opts.repertoire == "bin-base32":
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
113 encoder = base64.b32encode
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
114 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
115 rstrip_chars=b"=")
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
116 elif opts.repertoire == "bin-ascii85":
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
117 encoder = base64.a85encode
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
118 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder)
241
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
119 elif opts.repertoire == "bin-hex":
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
120 encoder = binascii.hexlify
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
121 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder)
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
122 else:
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
123 raise NotImplementedError("type not yet implemented: %s"
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
124 % opts.repertoire)
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
125 if opts.use_bin_length:
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
126 if len(pwd) < opts.req_length:
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
127 raise AssertionError("internal length mismatch")
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
128 else:
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
129 if len(pwd) != opts.req_length:
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
130 raise AssertionError("internal length mismatch")
234
c7dc57c44e8b FIX: For Python3: decode from ASCII before printing them: this removes the binary b'xxx' representation there
Franz Glasner <fzglas.hg@dom66.de>
parents: 233
diff changeset
131 if not PY2:
c7dc57c44e8b FIX: For Python3: decode from ASCII before printing them: this removes the binary b'xxx' representation there
Franz Glasner <fzglas.hg@dom66.de>
parents: 233
diff changeset
132 pwd = pwd.decode("ascii")
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
133 print(pwd)
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
134
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
135
239
1eae57292c7c genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 238
diff changeset
136 def gen_from_repertoire(length, repertoire):
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
137 """Select `length` characters randomly from given character repertoire
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
138 `repertoire`.
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
139
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
140 """
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
141 assert len(repertoire) <= 256
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
142 pwd = []
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
143 while len(pwd) < length:
238
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
144 rndbytes = os.urandom(16)
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
145 for c in rndbytes:
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
146 if PY2:
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
147 c = ord(c)
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
148 if c < len(repertoire):
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
149 pwd.append(repertoire[c])
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
150 if len(pwd) >= length:
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
151 break
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
152 if PY2:
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
153 pwd = b''.join(pwd)
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
154 else:
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
155 pwd = bytes(pwd)
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
156 return pwd
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
157
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
158
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
159 def gen_bin(length, use_bin_length, encoder, rstrip_chars=None):
240
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
160 """Generate from :func:`os.urandom` and just encode with given `encoder`.
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
161
32616df2732e Renamed algorithms/repertoire selection: use a "bin-" prefix when just the output of os.urandom() is encoded
Franz Glasner <fzglas.hg@dom66.de>
parents: 239
diff changeset
162 """
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
163 pwd = encoder(os.urandom(length))
235
11819361ea39 Implement option "-e" for genpwd to require a binary length instead of the output length.
Franz Glasner <fzglas.hg@dom66.de>
parents: 234
diff changeset
164 return pwd.rstrip(rstrip_chars) if use_bin_length else pwd[:length]
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
165
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
166
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
167 if __name__ == "__main__":
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
168 main()