Skip to content

Instantly share code, notes, and snippets.

@stantonk
Last active December 27, 2015 04:39
Show Gist options
  • Save stantonk/7268449 to your computer and use it in GitHub Desktop.
Save stantonk/7268449 to your computer and use it in GitHub Desktop.
generate cryptographically secure passwords in Python, and report the bits of entropy for a given length password based on the CHOICES you allow for random selection. random.SystemRandom() uses /dev/urandom on a *nix operating system, which should be cryptographically secure. There are some potential caveats / things I haven't explored fully, bu…
#!/usr/bin/env python
# http://stackoverflow.com/questions/5480131/will-python-systemrandom-os-urandom-always-have-enough-entropy-for-good-crypto
import math
import random
import sys
CHOICES = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$^&*()+/?,.'
if __name__ == '__main__':
if len(sys.argv) != 2:
print 'usage: %s password-length'
sys.exit(1)
pw_len = int(sys.argv[1])
pw = ''
for _ in xrange(pw_len):
pw += random.SystemRandom().choice(CHOICES)
print pw
print 'total bits of entropy: %s' % (math.log(len(CHOICES), 2) * pw_len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment