Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/genpwd.py @ 248:4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 08 Feb 2025 12:57:14 +0100 |
| parents | 435687f4e071 |
| children | f161448d673e |
| 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-._") |
| 246 | 36 # 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
|
37 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
|
38 b"23456789-._") |
| 246 | 39 # SAFE_WEB_CHARS with preference to punctuation |
| 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 | |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
49 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
50 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~") |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
51 # |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
52 # 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
|
53 # - 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
|
54 # - 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
|
55 # - 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
|
56 # - 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
|
57 SAFE_ASCII = (b"23456789ABCDEFGHJKLMNPQRSTUVWXYZ" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
58 b"abcdefghijkmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@_[]{|}~") |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
59 # just numeric and alphabetic |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
60 ALNUM = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
61 # 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
|
62 SAFE_ALNUM = b"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
63 |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
64 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
65 PY2 = sys.version_info[0] <= 2 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
66 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
67 |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
68 def main(argv=None): |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
69 aparser = argparse.ArgumentParser( |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
70 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
|
71 " length within a character repertoire", |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
72 fromfile_prefix_chars='@') |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
73 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
|
74 "--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
|
75 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
|
76 aparser.add_argument( |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
77 "--algorithm", "-a", |
| 246 | 78 choices=("web", "safe-web", "safe-web-2", |
| 79 "uri", "safe-uri", "safe-uri-2", | |
| 80 "ascii", "safe-ascii", | |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
81 "alnum", "safe-alnum", |
| 246 | 82 "bin-base64", "bin-urlsafe-base64", "bin-base32", "bin-hex", |
| 83 "bin-ascii85",), | |
|
245
35c06dcca856
genpwd: make the default "safe-ascii"
Franz Glasner <fzglas.hg@dom66.de>
parents:
244
diff
changeset
|
84 default="safe-ascii", |
|
242
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
85 help=""" |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
86 Select an algorithm and (implicitly) a character repertoire. |
|
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
87 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
|
88 "os.urandom()" with the selected encoder. |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
89 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
|
90 similar characters (currently `0O' or `Il1'). |
|
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
91 All repertoires that end with `-2' are variants with a bias to punctuation |
|
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
92 characters. |
|
247
435687f4e071
genpwd: FIX: Help message for the default
Franz Glasner <fzglas.hg@dom66.de>
parents:
246
diff
changeset
|
93 Default: safe-ascii |
|
242
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
94 """) |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
95 aparser.add_argument( |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
96 "-E", dest="use_bin_length", action="store_true", |
|
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
97 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
|
98 " 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
|
99 aparser.add_argument( |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
100 "req_length", metavar="OUTPUT-LENGTH", type=int, |
| 237 | 101 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
|
102 |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
103 opts = aparser.parse_args(args=argv) |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
104 |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
105 if opts.algorithm == "web": |
|
239
1eae57292c7c
genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
238
diff
changeset
|
106 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
107 elif opts.algorithm == "safe-web": |
|
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
|
108 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
109 elif opts.algorithm == "safe-web-2": |
|
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
|
110 pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS_2) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
111 elif opts.algorithm == "uri": |
| 246 | 112 pwd = gen_from_repertoire(opts.req_length, URI_CHARS) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
113 elif opts.algorithm == "safe-uri": |
| 246 | 114 pwd = gen_from_repertoire(opts.req_length, SAFE_URI_CHARS) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
115 elif opts.algorithm == "safe-uri-2": |
| 246 | 116 pwd = gen_from_repertoire(opts.req_length, SAFE_URI_CHARS_2) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
117 elif opts.algorithm == "ascii": |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
118 pwd = gen_from_repertoire(opts.req_length, FULL_ASCII) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
119 elif opts.algorithm == "safe-ascii": |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
120 pwd = gen_from_repertoire(opts.req_length, SAFE_ASCII) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
121 elif opts.algorithm == "alnum": |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
122 pwd = gen_from_repertoire(opts.req_length, ALNUM) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
123 elif opts.algorithm == "safe-alnum": |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
124 pwd = gen_from_repertoire(opts.req_length, SAFE_ALNUM) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
125 elif opts.algorithm == "bin-base64": |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
126 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
|
127 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
|
128 rstrip_chars=b"=") |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
129 elif opts.algorithm == "bin-urlsafe-base64": |
|
232
7ac8a2537bc9
Implement the urlsafe base64 character repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents:
231
diff
changeset
|
130 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
|
131 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
|
132 rstrip_chars=b"=") |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
133 elif opts.algorithm == "bin-base32": |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
134 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
|
135 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
|
136 rstrip_chars=b"=") |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
137 elif opts.algorithm == "bin-ascii85": |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
138 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
|
139 pwd = gen_bin(opts.req_length, opts.use_bin_length, encoder) |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
140 elif opts.algorithm == "bin-hex": |
|
241
d4501acb0a7c
Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents:
240
diff
changeset
|
141 encoder = binascii.hexlify |
|
d4501acb0a7c
Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents:
240
diff
changeset
|
142 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
|
143 else: |
|
248
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
144 raise NotImplementedError("algorithm not yet implemented: %s" |
|
4796e5da04ee
genpwd: Rename "--repertoire" to "--algorithm"
Franz Glasner <fzglas.hg@dom66.de>
parents:
247
diff
changeset
|
145 % opts.algorithm) |
|
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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 pwd = pwd.decode("ascii") |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
154 print(pwd) |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
155 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
156 |
|
239
1eae57292c7c
genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
238
diff
changeset
|
157 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
|
158 """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
|
159 `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
|
160 |
|
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 """ |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
162 assert len(repertoire) <= 256 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
163 pwd = [] |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
164 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
|
165 rndbytes = os.urandom(16) |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
166 for c in rndbytes: |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
167 if PY2: |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
168 c = ord(c) |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
169 if c < len(repertoire): |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
170 pwd.append(repertoire[c]) |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
171 if len(pwd) >= length: |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
172 break |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
173 if PY2: |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
174 pwd = b''.join(pwd) |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
175 else: |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
176 pwd = bytes(pwd) |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
177 return pwd |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
178 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
179 |
|
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
|
180 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
|
181 """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
|
182 |
|
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
|
183 """ |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
184 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
|
185 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
|
186 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
187 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
188 if __name__ == "__main__": |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
189 main() |
