diff 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
line wrap: on
line diff
--- a/cutils/genpwd.py	Sat Feb 08 08:53:18 2025 +0100
+++ b/cutils/genpwd.py	Sat Feb 08 08:54:02 2025 +0100
@@ -94,19 +94,18 @@
     print(pwd)
 
 
-def gen_web(length, chars):
-    mult = 256//len(chars)
-    repertoire = chars * mult
+def gen_web(length, repertoire):
     assert len(repertoire) <= 256
     pwd = []
     while len(pwd) < length:
-        c = os.urandom(1)
-        if PY2:
-            c = ord(c)
-        else:
-            c = c[0]
-        if c < len(repertoire):
-            pwd.append(repertoire[c])
+        rndbytes = os.urandom(16)
+        for c in rndbytes:
+            if PY2:
+                c = ord(c)
+            if c < len(repertoire):
+                pwd.append(repertoire[c])
+                if len(pwd) >= length:
+                    break
     if PY2:
         pwd = b''.join(pwd)
     else: