Mercurial > hgrepos > Python > apps > py-cutils
comparison genpwd.py @ 227:4bb2d0975cfe imports
imports: import genpwd,py from fmgbackup4.
This is a script to generate passwords.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 07 Feb 2025 12:37:13 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 227:4bb2d0975cfe |
|---|---|
| 1 #!/usr/local/bin/python3 | |
| 2 # -*- coding: utf-8 -*- | |
| 3 # | |
| 4 # @(#)$Id: //Administration/Server/Konfiguration/Dateien/_main/fmgbackup4/root/bin/genpwd.py#1 $ | |
| 5 # $Change: 28588 $ $DateTime: 2018/10/31 13:11:48 $ $Author: fag $ | |
| 6 # | |
| 7 r"""Generate passwords. | |
| 8 | |
| 9 Usage: genpwd.py [ Options ] required_length | |
| 10 | |
| 11 Options: | |
| 12 | |
| 13 --type, -t web, web-safe, web-safe2, base64, base32, ascii85 | |
| 14 | |
| 15 :Author: Franz Glasner | |
| 16 | |
| 17 """ | |
| 18 | |
| 19 from __future__ import (division, absolute_import, print_function) | |
| 20 | |
| 21 __author__ = "Franz Glasner" | |
| 22 | |
| 23 __version__ = "0.1" | |
| 24 | |
| 25 | |
| 26 import getopt | |
| 27 import sys | |
| 28 import os | |
| 29 import base64 | |
| 30 | |
| 31 | |
| 32 WEB_CHARS = b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz0123456789.,-_;!()[]{}*" | |
| 33 WEB_SAFE_CHARS = b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz23456789.,-_;!" | |
| 34 WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS | |
| 35 | |
| 36 PY2 = sys.version_info[0] <= 2 | |
| 37 | |
| 38 | |
| 39 def main(): | |
| 40 opt_type = "web-safe2" | |
| 41 opts, args = getopt.getopt(sys.argv[1:], | |
| 42 "t:", | |
| 43 ["type="]) | |
| 44 for opt, val in opts: | |
| 45 if opt in ("-t", "--type"): | |
| 46 if val not in ("web", "web-safe", "web-safe2", | |
| 47 "base64", "base32", "ascii85", ): | |
| 48 raise getopt.GetoptError("invalid type: %s" % val, "") | |
| 49 opt_type = val | |
| 50 else: | |
| 51 raise RuntimeError("inconsistent getopt handling") | |
| 52 | |
| 53 try: | |
| 54 req_length = int(args[0], 10) | |
| 55 except IndexError: | |
| 56 raise getopt.GetoptError("no length given") | |
| 57 | |
| 58 if opt_type == "web": | |
| 59 pwd = gen_web(req_length, WEB_CHARS) | |
| 60 elif opt_type == "web-safe": | |
| 61 pwd = gen_web(req_length, WEB_SAFE_CHARS) | |
| 62 elif opt_type == "web-safe2": | |
| 63 pwd = gen_web(req_length, WEB_SAFE2_CHARS) | |
| 64 elif opt_type == "base64": | |
| 65 encoder = base64.b64encode | |
| 66 pwd = gen_bin(req_length, encoder) | |
| 67 elif opt_type == "base32": | |
| 68 encoder = base64.b32encode | |
| 69 pwd = gen_bin(req_length, encoder) | |
| 70 elif opt_type == "ascii85": | |
| 71 encoder = base64.a85encode | |
| 72 pwd = gen_bin(req_length, encoder) | |
| 73 else: | |
| 74 raise NotImplementedError("type not yet implemented: %s" % opt_type) | |
| 75 print(pwd) | |
| 76 | |
| 77 | |
| 78 def gen_web(length, chars): | |
| 79 mult = 256//len(chars) | |
| 80 repertoire = chars * mult | |
| 81 assert len(repertoire) <= 256 | |
| 82 pwd = [] | |
| 83 while len(pwd) < length: | |
| 84 c = os.urandom(1) | |
| 85 if PY2: | |
| 86 c = ord(c) | |
| 87 else: | |
| 88 c = c[0] | |
| 89 if c < len(repertoire): | |
| 90 pwd.append(repertoire[c]) | |
| 91 if PY2: | |
| 92 pwd = b''.join(pwd) | |
| 93 else: | |
| 94 pwd = bytes(pwd) | |
| 95 return pwd | |
| 96 | |
| 97 | |
| 98 def gen_bin(length, encoder): | |
| 99 pwd = encoder(os.urandom(length)) | |
| 100 return pwd[:length] | |
| 101 | |
| 102 | |
| 103 if __name__ == "__main__": | |
| 104 main() |
