I'm writing a web service which stores data which will be shared between two separate systems.
/session/requestNewSession?args=<data> => returns session id
/session/requestArgs?session=<session id> => returns <data> stored with key <session id>
The code is implemented in python in the twisted matrix library, and I've written my own session id generator:
private_secret = os.urandom(64)
def generateRandomSessionKey():
rawdata = private_secret + str(time.time()) + string.join(map(chr, [random.randint(0,255) for x in range(100)]),"")
session_key = hashlib.sha256(rawdata).hexdigest()
del(rawdata)
return session_key
Is this a proper way of generating a secure session id (unguessable)? If not, any ideas on what I should do differently?
os.urandom()will never block. At least, not on any OS platform I am familiar with. (On Unix, internally it uses/dev/urandom, not/dev/random./dev/urandomdoesn't block -- you may be thinking of/dev/random, which can block, but you should always use/dev/urandomnot/dev/random. On Windows, it usesCryptGenRandom(), which should never block, either.) – D.W. Apr 30 '12 at 22:21