1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
"""
Create random secrets.
"""
import base64
import os
import random
def random_bytes(length):
"""
Return a string of the given length. Uses ``os.urandom`` if it
can, or just pseudo-random numbers otherwise.
"""
try:
return os.urandom(length)
except AttributeError:
return b''.join([
bytes((random.randrange(256),)) for i in range(length)])
def secret_string(length=25):
"""
Returns a random string of the given length. The string
is a base64-encoded version of a set of random bytes, truncated
to the given length (and without any newlines).
"""
s = random_bytes(length)
s = base64.b64encode(s)
s = s.decode('ascii')
for badchar in '\n\r=':
s = s.replace(badchar, '')
# We're wasting some characters here. But random characters are
# cheap ;)
return s[:length]
|