Guest User

Password Generation Script

a guest
Feb 1st, 2013
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # This is public domain software.  No warranty is implied.
  4. # It may not be suitable for any purpose.
  5.  
  6. import os
  7. import sys
  8. import string
  9. from math import log, ceil
  10. import time
  11. import random
  12.  
  13. def get_bits(n, generator = os.urandom):
  14.   byte_count = (n+7)//8
  15.   data = generator(byte_count)
  16.   return int(''.join(['%02x' % x for x in data]), 16)
  17.  
  18. ALPHABET_32 = string.ascii_lowercase + '234567'
  19. ALPHABET_62 = string.ascii_letters + string.digits
  20. ALPHABET_84 = ALPHABET_62 + string.punctuation
  21.  
  22. ALPHABETS = [string.ascii_letters, ALPHABET_62, ALPHABET_84, ALPHABET_32]
  23.  
  24. def create_password(bits, alphabet = ALPHABET_62, generator = os.urandom):
  25.   alphabet_size = len(alphabet)
  26.   length = ceil(log(1<<bits) / log(alphabet_size))
  27.   bits = ceil(log(alphabet_size**length) / log(2))
  28.   seed = get_bits(bits+32, generator) # reduce bias by getting extra bits
  29.   result = ''
  30.   while len(result) < length:
  31.     result += alphabet[seed % alphabet_size]
  32.     seed //= alphabet_size
  33.   return result
  34.  
  35. if __name__ == '__main__':
  36.   args = sys.argv[1:]
  37.   if len(args) > 0:
  38.     assert(len(args) >= 2 and len(args) <= 4)
  39.     alphabet = ALPHABET_62
  40.     if len(args) < 3:
  41.       args = [args[0], args[0], args[1]]
  42.     elif len(args) >= 4:
  43.       if args[2] == '84':
  44.         alphabet = ALPHABET_84
  45.     path = os.path.join(os.getenv('HOME'), 'Crypto', 'Passwords', args[2] + '.gpg')
  46.     print(path)
  47.     assert not os.path.exists(path)
  48.     pw = create_password(80, alphabet, (args[0]+args[1]+args[2]).encode('utf8'))
  49.     pipe = os.popen('gpg -e -r %s > "%s"' % (os.getenv('USER'), path), 'w')
  50.     pipe.write('%s\n%s\n%s\n' % (args[1], args[0], pw))
  51.     pipe.close()
  52.  
  53.   while True:
  54.     print(create_password(80, random.choice(ALPHABETS)))
  55.     time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment