diff options
author | Justin Santa Barbara <justin@fathomdb.com> | 2011-03-15 11:24:07 -0700 |
---|---|---|
committer | Justin Santa Barbara <justin@fathomdb.com> | 2011-03-15 11:24:07 -0700 |
commit | 22aad6700124411aceed0b2bd3953cbbc48b6130 (patch) | |
tree | 78bce25a23e6913252b542922e99b34bb738a383 /nova/utils.py | |
parent | 3d0cde272e3227978c5875c811c93e1e3df692ed (diff) | |
download | nova-22aad6700124411aceed0b2bd3953cbbc48b6130.tar.gz nova-22aad6700124411aceed0b2bd3953cbbc48b6130.tar.xz nova-22aad6700124411aceed0b2bd3953cbbc48b6130.zip |
Use random.SystemRandom for easy secure randoms, configurable symbol set by default including mixed-case
Diffstat (limited to 'nova/utils.py')
-rw-r--r-- | nova/utils.py | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/nova/utils.py b/nova/utils.py index 0510c3cbe..199ee8701 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -262,19 +262,25 @@ def generate_mac(): return ':'.join(map(lambda x: "%02x" % x, mac)) -def generate_password(length=20): - """Generate a random alphanumeric password, avoiding 'confusing' O,0,I,1. +# Default symbols to use for passwords. Avoids visually confusing characters. +# ~6 bits per symbol +DEFAULT_PASSWORD_SYMBOLS = ("23456789" # Removed: 0,1 + "ABCDEFGHJKLMNPQRSTUVWXYZ" # Removed: I, O + "abcdefghijkmnopqrstuvwxyz") # Removed: l + + +# ~5 bits per symbol +EASIER_PASSWORD_SYMBOLS = ("23456789" # Removed: 0, 1 + "ABCDEFGHJKLMNPQRSTUVWXYZ") # Removed: I, O + + +def generate_password(length=20, symbols=DEFAULT_PASSWORD_SYMBOLS): + """Generate a random password from the supplied symbols. Believed to be reasonably secure (with a reasonable password length!) """ - # 26 letters, 10 digits = 36 choices - # Remove O, 0, I, 1 => 32 choices - # 32 choices means we're just using the low 5 bit of each byte, - # so there's no bias introduced by using a modulo - chrs = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" - - random_bytes = os.urandom(length) - return "".join([chrs[ord(random_bytes[i]) % 32] for i in xrange(length)]) + r = random.SystemRandom() + return "".join([r.choice(symbols) for _i in xrange(length)]) def last_octet(address): |