Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/genpwd.py @ 243:86417af99561
genpwd: Implement ascii and alnum and their safe variants
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 08 Feb 2025 10:39:49 +0100 |
| parents | ae9bc3006efc |
| children | 42f7ecd70ec1 |
| 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 |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
30 WEB_CHARS = (b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
31 b"0123456789.,-_;!()[]{}*") |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
32 WEB_SAFE_CHARS = (b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
33 b"23456789.,-_;!") |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
34 WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_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
|
35 # 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
|
36 FULL_ASCII = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
37 b"abcdefghijklmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@^_`[\\]{|}'\"~") |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
38 # |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
39 # 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
|
40 # - 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
|
41 # - 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
|
42 # - 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
|
43 # - 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
|
44 SAFE_ASCII = (b"23456789ABCDEFGHJKLMNPQRSTUVWXYZ" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
45 b"abcdefghijkmnopqrstuvwxyz!#$%&/()*+-.,:;<=>?@_[]{|}~") |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
46 # just numeric and alphabetic |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
47 ALNUM = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
48 # 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
|
49 SAFE_ALNUM = b"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
50 |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
51 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
52 PY2 = sys.version_info[0] <= 2 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
53 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
54 |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
55 def main(argv=None): |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
56 aparser = argparse.ArgumentParser( |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
57 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
|
58 " length within a character repertoire", |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
59 fromfile_prefix_chars='@') |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
60 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
|
61 "--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
|
62 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
|
63 aparser.add_argument( |
|
236
939a8da6bc92
genpwd: change the current "-e" option to "-E"
Franz Glasner <fzglas.hg@dom66.de>
parents:
235
diff
changeset
|
64 "-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
|
65 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
|
66 " 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
|
67 aparser.add_argument( |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
68 "--repertoire", "--type", "-t", |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
69 choices=("web", "web-safe", "web-safe2", "ascii", "safe-ascii", |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
70 "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
|
71 "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
|
72 "bin-ascii85", "bin-hex", ), |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
73 default="web-safe2", |
|
242
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
74 help=""" |
|
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
75 Select from a character repertoire. |
|
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
76 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
|
77 "os.urandom()" with the selected encoder. |
|
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
78 Default: web-safe2 |
|
ae9bc3006efc
Enhanced help message: describe the "bin-" prefix
Franz Glasner <fzglas.hg@dom66.de>
parents:
241
diff
changeset
|
79 """) |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
80 aparser.add_argument( |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
81 "req_length", metavar="OUTPUT-LENGTH", type=int, |
| 237 | 82 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
|
83 |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
84 opts = aparser.parse_args(args=argv) |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
85 |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
86 if opts.repertoire == "web": |
|
239
1eae57292c7c
genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
238
diff
changeset
|
87 pwd = gen_from_repertoire(opts.req_length, WEB_CHARS) |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
88 elif opts.repertoire == "web-safe": |
|
239
1eae57292c7c
genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
238
diff
changeset
|
89 pwd = gen_from_repertoire(opts.req_length, WEB_SAFE_CHARS) |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
90 elif opts.repertoire == "web-safe2": |
|
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_SAFE2_CHARS) |
|
243
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
92 elif opts.repertoire == "ascii": |
|
86417af99561
genpwd: Implement ascii and alnum and their safe variants
Franz Glasner <fzglas.hg@dom66.de>
parents:
242
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 elif opts.repertoire == "alnum": |
|
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, ALNUM) |
|
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-alnum": |
|
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_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
|
100 elif opts.repertoire == "bin-base64": |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 elif opts.repertoire == "bin-urlsafe-base64": |
|
232
7ac8a2537bc9
Implement the urlsafe base64 character repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents:
231
diff
changeset
|
105 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
|
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-base32": |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
109 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
|
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-ascii85": |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
113 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
|
114 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
|
115 elif opts.repertoire == "bin-hex": |
|
d4501acb0a7c
Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents:
240
diff
changeset
|
116 encoder = binascii.hexlify |
|
d4501acb0a7c
Implement a "bin-hex" password selection/repertoire
Franz Glasner <fzglas.hg@dom66.de>
parents:
240
diff
changeset
|
117 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
|
118 else: |
|
231
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
119 raise NotImplementedError("type not yet implemented: %s" |
|
6d8443878a00
Use argparse in genpwd.py now.
Franz Glasner <fzglas.hg@dom66.de>
parents:
230
diff
changeset
|
120 % 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 pwd = pwd.decode("ascii") |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
129 print(pwd) |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
130 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
131 |
|
239
1eae57292c7c
genpwd: rename "gen_web()" to "gen_from_repertoire()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
238
diff
changeset
|
132 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
|
133 """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
|
134 `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
|
135 |
|
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
|
136 """ |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
137 assert len(repertoire) <= 256 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
138 pwd = [] |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
139 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
|
140 rndbytes = os.urandom(16) |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
141 for c in rndbytes: |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
142 if PY2: |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
143 c = ord(c) |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
144 if c < len(repertoire): |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
145 pwd.append(repertoire[c]) |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
146 if len(pwd) >= length: |
|
ff13b2a863ba
Make selection from a character repertoire more evenly distributed
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
147 break |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
148 if PY2: |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
149 pwd = b''.join(pwd) |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
150 else: |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
151 pwd = bytes(pwd) |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
152 return pwd |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
153 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
154 |
|
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
|
155 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
|
156 """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
|
157 |
|
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 """ |
|
227
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
159 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
|
160 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
|
161 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
162 |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
163 if __name__ == "__main__": |
|
4bb2d0975cfe
imports: import genpwd,py from fmgbackup4.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
164 main() |
