Skip to content

Instantly share code, notes, and snippets.

@namsral
Created March 18, 2014 15:06
Show Gist options
  • Save namsral/9621883 to your computer and use it in GitHub Desktop.
Save namsral/9621883 to your computer and use it in GitHub Desktop.
Random password generator with a default entropy of 84 bits.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, string
def random_password(length=14):
"""
Random password generator.
Default generated password will have an entropy of 84 bits.
For each character in the password, generate a random byte, reduce the
integer value to modulo 64 and use the result as an index on the character
pool.
In a 64 character pool, each character will have an entropy of 6 bits.
With a lenght of 14 characters the total entropy is 84 bits:
log2(64) * 14 = 84 bits
The difference between 13 and 14 characters can be several years to
brute force.
"""
characters = string.ascii_letters + string.digits + '+/'
l = list()
for x in xrange(length):
i = ord(os.urandom(1))%len(characters)
c = characters[i]
l.append(c)
return ''.join(l)
if __name__ == '__main__':
print(random_password())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment