Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/genpwd.py @ 238:ff13b2a863ba
Make selection from a character repertoire more evenly distributed
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 08 Feb 2025 08:54:02 +0100 |
| parents | d65a4d861dad |
| children | 1eae57292c7c |
comparison
equal
deleted
inserted
replaced
| 237:d65a4d861dad | 238:ff13b2a863ba |
|---|---|
| 92 if not PY2: | 92 if not PY2: |
| 93 pwd = pwd.decode("ascii") | 93 pwd = pwd.decode("ascii") |
| 94 print(pwd) | 94 print(pwd) |
| 95 | 95 |
| 96 | 96 |
| 97 def gen_web(length, chars): | 97 def gen_web(length, repertoire): |
| 98 mult = 256//len(chars) | |
| 99 repertoire = chars * mult | |
| 100 assert len(repertoire) <= 256 | 98 assert len(repertoire) <= 256 |
| 101 pwd = [] | 99 pwd = [] |
| 102 while len(pwd) < length: | 100 while len(pwd) < length: |
| 103 c = os.urandom(1) | 101 rndbytes = os.urandom(16) |
| 104 if PY2: | 102 for c in rndbytes: |
| 105 c = ord(c) | 103 if PY2: |
| 106 else: | 104 c = ord(c) |
| 107 c = c[0] | 105 if c < len(repertoire): |
| 108 if c < len(repertoire): | 106 pwd.append(repertoire[c]) |
| 109 pwd.append(repertoire[c]) | 107 if len(pwd) >= length: |
| 108 break | |
| 110 if PY2: | 109 if PY2: |
| 111 pwd = b''.join(pwd) | 110 pwd = b''.join(pwd) |
| 112 else: | 111 else: |
| 113 pwd = bytes(pwd) | 112 pwd = bytes(pwd) |
| 114 return pwd | 113 return pwd |
