annotate cutils/genpwd.py @ 264:c657828cf62f

Remove executable flag for cutils/genpwd.py
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 16 Feb 2025 23:57:12 +0100
parents 655f9e4bc6f2
children 48430941c18c
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 # :-
252
0a2a162c5ad7 genpwd: Wording in help and docs
Franz Glasner <fzglas.hg@dom66.de>
parents: 251
diff changeset
7 r"""A simple password generator to generate random passwords from selected
0a2a162c5ad7 genpwd: Wording in help and docs
Franz Glasner <fzglas.hg@dom66.de>
parents: 251
diff changeset
8 character repertoires.
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
9
251
14bb7423445d genpwd: module description
Franz Glasner <fzglas.hg@dom66.de>
parents: 250
diff changeset
10 Use :command:`genpwd.py --help' for a detailed help message.
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
11
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
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
14 from __future__ import (division, absolute_import, print_function)
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
15
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
16 import argparse
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
17 import base64
241
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
18 import binascii
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
19 import os
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
20 import sys
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21
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
22 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
23
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
24
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
25 #
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
26 # 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
27 # 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
28 #
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
29 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
30 b"0123456789-._")
246
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
31 # WEB_CHARS without visually similar characters (0O, 1lI)
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
32 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
33 b"23456789-._")
246
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
34 # SAFE_WEB_CHARS with preference to punctuation
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
35 SAFE_WEB_CHARS_2 = b".-_" + SAFE_WEB_CHARS
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
36 # Unreserved characters from URI but with sub-delims allowed
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
37 URI_CHARS = WEB_CHARS + b"~" + b"!$&'()*+,;="
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
38 # URI_CHARS without visually similar characters
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
39 SAFE_URI_CHARS = SAFE_WEB_CHARS + b"~" + b"!$&'()*+,;="
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
40 # Just like SAFE_URI_CHARS but prefers punctuation characters
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
41 SAFE_URI_CHARS_2 = (b"~" + b"!$&'()*+,;=" + SAFE_WEB_CHARS
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
42 + b"~" + b"!$&'()*+,;=")
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
43 # All visible characters from ASCII character set but no space
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
44 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
45 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~")
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
46 #
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
47 # 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
48 # - 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
49 # - 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
50 # - 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
51 # - 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
52 SAFE_ASCII = (b"23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
53 b"abcdefghijkmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@_[]{|}~")
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
54 # just numeric and alphabetic
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
55 ALNUM = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
56 # 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
57 SAFE_ALNUM = b"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
58
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
59
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
60 PY2 = sys.version_info[0] <= 2
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
61
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
62
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
63 def main(argv=None):
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
64 aparser = argparse.ArgumentParser(
252
0a2a162c5ad7 genpwd: Wording in help and docs
Franz Glasner <fzglas.hg@dom66.de>
parents: 251
diff changeset
65 description="A simple password generator for passwords with a given"
0a2a162c5ad7 genpwd: Wording in help and docs
Franz Glasner <fzglas.hg@dom66.de>
parents: 251
diff changeset
66 " length within a selected character repertoire",
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
67 fromfile_prefix_chars='@')
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
68 aparser.add_argument(
253
4314ee20927a genpwd: more consistency with regard to help messages
Franz Glasner <fzglas.hg@dom66.de>
parents: 252
diff changeset
69 "-v", "--version", action="version",
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
70 version="%s (rv:%s)" % (__version__, __revision__))
249
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
71 group = aparser.add_mutually_exclusive_group()
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
72 group.add_argument(
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
73 "--algorithm", "-a",
246
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
74 choices=("web", "safe-web", "safe-web-2",
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
75 "uri", "safe-uri", "safe-uri-2",
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
76 "ascii", "safe-ascii",
243
86417af99561 genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents: 242
diff changeset
77 "alnum", "safe-alnum",
246
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
78 "bin-base64", "bin-urlsafe-base64", "bin-base32", "bin-hex",
c29266444003 genpwd: Implement "uri"
Franz Glasner <fzglas.hg@dom66.de>
parents: 245
diff changeset
79 "bin-ascii85",),
245
35c06dcca856 genpwd: make the default "safe-ascii"
Franz Glasner <fzglas.hg@dom66.de>
parents: 244
diff changeset
80 default="safe-ascii",
242
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
81 help="""
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
82 Select an algorithm and (implicitly) a character repertoire.
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
83 All repertoires that start with `bin-' just encode the output of
242
ae9bc3006efc Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents: 241
diff changeset
84 "os.urandom()" with the selected encoder.
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
85 All repertoires that end with `-safe' or `safe-2' do not contain visually
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
86 similar characters (currently `0O' or `Il1').
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
87 All repertoires that end with `-2' are variants with a bias to punctuation
249
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
88 characters.
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
89 This is incompatible with option `--repertoire'.
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
90 Default: safe-ascii""")
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
91 group.add_argument(
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
92 "--repertoire", "-r",
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
93 action="store", metavar="REPERTOIRE",
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
94 help="""
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
95 Select from given character repertoire.
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
96 The repertoire must be characters from the ISO-8859-15 character set.
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
97 An empty REPERTOIRE selects implicitly the default algorithm.
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
98 This is incompatible with option `--algorithm'.""")
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
99 aparser.add_argument(
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
100 "-E", dest="use_bin_length", action="store_true",
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
101 help="For some repertoires make OUTPUT-LENGTH the number of bytes"
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
102 " that is to be read from random sources instead of output bytes")
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
103 aparser.add_argument(
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
104 "--group", "-G", action="store_true",
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
105 help="""Group the results. If no "--group-sep" or "--group-size" is
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
106 given use their respective defaults.""")
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
107 aparser.add_argument(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
108 "--group-sep", action="store", default=None, metavar="GROUP-SEP",
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
109 help="""Group the result using GROUP-SEP as separator.
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
110 Option "--group" is implied.
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
111 Default when grouping is enabled is the SPACE character ` '.""")
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
112 aparser.add_argument(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
113 "--group-size", action="store", type=int, default=None,
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
114 metavar="GROUP-SIZE",
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
115 help="""Group the result using a group size of GROUP-SIZE characters.
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
116 Option "--group" is implied.
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
117 Default when grouping is enabled is 6.""")
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
118 aparser.add_argument(
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
119 "req_length", metavar="OUTPUT-LENGTH", type=int,
237
d65a4d861dad Enhance help message
Franz Glasner <fzglas.hg@dom66.de>
parents: 236
diff changeset
120 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
121
231
6d8443878a00 Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents: 230
diff changeset
122 opts = aparser.parse_args(args=argv)
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
123
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
124 grouper = None
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
125 if opts.group or opts.group_sep is not None or opts.group_size is not None:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
126 if opts.group_sep is None:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
127 gsep = b' '
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
128 else:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
129 if PY2:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
130 gsep = opts.group_sep
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
131 else:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
132 gsep = opts.group_sep.encode("utf8")
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
133 gsize = 6 if opts.group_size is None else opts.group_size
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
134 grouper = make_grouper(sep=gsep, size=gsize)
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
135
249
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
136 if opts.repertoire:
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
137 try:
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
138 repertoire = (opts.repertoire
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
139 if PY2
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
140 else opts.repertoire.encode("iso-8859-15"))
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
141 except UnicodeError:
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
142 raise ValueError("non ISO-8859-15 character in given repertoire")
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
143 pwd = gen_from_repertoire(opts.req_length, repertoire, grouper=grouper)
249
f161448d673e genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
144 elif opts.algorithm == "web":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
145 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
146 opts.req_length, WEB_CHARS, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
147 elif opts.algorithm == "safe-web":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
148 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
149 opts.req_length, SAFE_WEB_CHARS, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
150 elif opts.algorithm == "safe-web-2":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
151 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
152 opts.req_length, SAFE_WEB_CHARS_2, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
153 elif opts.algorithm == "uri":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
154 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
155 opts.req_length, URI_CHARS, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
156 elif opts.algorithm == "safe-uri":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
157 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
158 opts.req_length, SAFE_URI_CHARS, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
159 elif opts.algorithm == "safe-uri-2":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
160 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
161 opts.req_length, SAFE_URI_CHARS_2, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
162 elif opts.algorithm == "ascii":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
163 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
164 opts.req_length, FULL_ASCII, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
165 elif opts.algorithm == "safe-ascii":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
166 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
167 opts.req_length, SAFE_ASCII, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
168 elif opts.algorithm == "alnum":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
169 pwd = gen_from_repertoire(opts.req_length, ALNUM, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
170 elif opts.algorithm == "safe-alnum":
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
171 pwd = gen_from_repertoire(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
172 opts.req_length, SAFE_ALNUM, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
173 elif opts.algorithm == "bin-base64":
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
174 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
175 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
176 rstrip_chars=b"=", grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
177 elif opts.algorithm == "bin-urlsafe-base64":
232
7ac8a2537bc9 Implement the urlsafe base64 character repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 231
diff changeset
178 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
179 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
180 rstrip_chars=b"=", grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
181 elif opts.algorithm == "bin-base32":
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
182 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
183 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder,
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
184 rstrip_chars=b"=", grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
185 elif opts.algorithm == "bin-ascii85":
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
186 encoder = base64.a85encode
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
187 pwd = gen_bin(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
188 opts.req_length, opts.use_bin_length, encoder, grouper=grouper)
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
189 elif opts.algorithm == "bin-hex":
241
d4501acb0a7c Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents: 240
diff changeset
190 encoder = binascii.hexlify
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
191 pwd = gen_bin(
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
192 opts.req_length, opts.use_bin_length, encoder, grouper=grouper)
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
193 else:
248
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
194 raise NotImplementedError("algorithm not yet implemented: %s"
4796e5da04ee genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents: 247
diff changeset
195 % opts.algorithm)
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
196 if opts.group or opts.group_size or opts.group_sep:
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
197 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
198 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
199 else:
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
200 if opts.use_bin_length:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
201 if len(pwd) < opts.req_length:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
202 raise AssertionError("internal length mismatch")
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
203 else:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
204 if len(pwd) != opts.req_length:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
205 raise AssertionError("internal length mismatch")
250
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
206 if PY2:
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
207 print(pwd)
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
208 sys.stdout.flush()
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
209 else:
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
210 sys.stdout.buffer.write(pwd)
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
211 sys.stdout.buffer.write(b'\n')
48f89d312309 genpwd: use binary output on Python3 to prevent double encoding
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
212 sys.stdout.buffer.flush()
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
213
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
214
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
215 def gen_from_repertoire(length, repertoire, grouper=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
216 """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
217 `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
218
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
219 """
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
220 assert len(repertoire) <= 256
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
221 pwd = []
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
222 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
223 rndbytes = os.urandom(16)
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
224 for c in rndbytes:
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
225 if PY2:
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
226 c = ord(c)
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
227 if c < len(repertoire):
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
228 pwd.append(repertoire[c])
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
229 if len(pwd) >= length:
ff13b2a863ba Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
230 break
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
231 if PY2:
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
232 pwd = b''.join(pwd)
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
233 else:
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
234 pwd = bytes(pwd)
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
235 if grouper:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
236 pwd = grouper(pwd)
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
237 return pwd
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
238
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
239
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
240 def gen_bin(length, use_bin_length, encoder, rstrip_chars=None, grouper=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
241 """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
242
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
243 """
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
244 pwd = encoder(os.urandom(length))
254
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
245 pwd = pwd.rstrip(rstrip_chars) if use_bin_length else pwd[:length]
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
246 if grouper:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
247 pwd = grouper(pwd)
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
248 return pwd
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
249
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
250
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
251 def make_grouper(sep=b' ', size=6):
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
252
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
253 def _grouper(pwd):
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
254 if not pwd or size <= 0:
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
255 return pwd
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
256 assert isinstance(pwd, bytes)
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
257 groups = []
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
258 idx = 0
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
259 while idx < len(pwd):
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
260 groups.append(pwd[idx:idx+size])
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
261 idx += size
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
262 return sep.join(groups)
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
263
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
264 assert isinstance(sep, bytes)
655f9e4bc6f2 genpwd: allow grouping of the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 253
diff changeset
265 return _grouper
227
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
266
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
267
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
268 if __name__ == "__main__":
4bb2d0975cfe imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
269 main()