Commits

Jakub Wilk committed e17e7b9

When generating random passwords, use random.SystemRandom() instead of spawning gpg.

  • Participants
  • Parent commits c14537d

Comments (0)

Files changed (1)

File password-manager

 import argparse
 import math
 import os
+import random
 import subprocess as ipc
 import sys
 import textwrap
         char_range('0', '9')
     )
     chunk_bits = math.log(len(alphabet), 2) * options.length
-    chunk_bytes = int(math.ceil(chunk_bits / 8))
     chunk_bits = int(math.ceil(chunk_bits))
-    gpg = ipc.Popen(['gpg', '--gen-random', '1'], stdout=ipc.PIPE)
-    try:
-        for i in range(options.count):
-            while True:
-                blob = gpg.stdout.read(chunk_bytes)
-                bigint = 0
-                for byte in blob:
-                    bigint <<= 8
-                    bigint |= byte
-                bigint &= (1 << chunk_bits) - 1
-                if bigint >= len(alphabet) ** options.length:
-                    continue
-                password = [None] * options.length
-                for j in range(options.length):
-                    bigint, password[j] = divmod(bigint, len(alphabet))
-                assert bigint == 0
-                password = ''.join(alphabet[ch] for ch in password)
-                print(password)
+    sysrandom = random.SystemRandom()
+    for i in range(options.count):
+        while True:
+            bigint = sysrandom.getrandbits(chunk_bits)
+            if bigint < len(alphabet) ** options.length:
                 break
-    finally:
-        # Ugh, .stdout.close() is not enough and .terminate() would generate an
-        # ugly error message.
-        gpg.kill()
-        gpg.wait()
+        password = [None] * options.length
+        for j in range(options.length):
+            bigint, password[j] = divmod(bigint, len(alphabet))
+        assert bigint == 0
+        password = ''.join(alphabet[ch] for ch in password)
+        print(password)
 
 if __name__ == '__main__':
     main()