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