Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # This is public domain software. No warranty is implied.
- # It may not be suitable for any purpose.
- import os
- import sys
- import string
- from math import log, ceil
- import time
- import random
- def get_bits(n, generator = os.urandom):
- byte_count = (n+7)//8
- data = generator(byte_count)
- return int(''.join(['%02x' % x for x in data]), 16)
- ALPHABET_32 = string.ascii_lowercase + '234567'
- ALPHABET_62 = string.ascii_letters + string.digits
- ALPHABET_84 = ALPHABET_62 + string.punctuation
- ALPHABETS = [string.ascii_letters, ALPHABET_62, ALPHABET_84, ALPHABET_32]
- def create_password(bits, alphabet = ALPHABET_62, generator = os.urandom):
- alphabet_size = len(alphabet)
- length = ceil(log(1<<bits) / log(alphabet_size))
- bits = ceil(log(alphabet_size**length) / log(2))
- seed = get_bits(bits+32, generator) # reduce bias by getting extra bits
- result = ''
- while len(result) < length:
- result += alphabet[seed % alphabet_size]
- seed //= alphabet_size
- return result
- if __name__ == '__main__':
- args = sys.argv[1:]
- if len(args) > 0:
- assert(len(args) >= 2 and len(args) <= 4)
- alphabet = ALPHABET_62
- if len(args) < 3:
- args = [args[0], args[0], args[1]]
- elif len(args) >= 4:
- if args[2] == '84':
- alphabet = ALPHABET_84
- path = os.path.join(os.getenv('HOME'), 'Crypto', 'Passwords', args[2] + '.gpg')
- print(path)
- assert not os.path.exists(path)
- pw = create_password(80, alphabet, (args[0]+args[1]+args[2]).encode('utf8'))
- pipe = os.popen('gpg -e -r %s > "%s"' % (os.getenv('USER'), path), 'w')
- pipe.write('%s\n%s\n%s\n' % (args[1], args[0], pw))
- pipe.close()
- while True:
- print(create_password(80, random.choice(ALPHABETS)))
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment