Ticket #1180: ticket_1180__rev_8168-getrandbits.diff
File ticket_1180__rev_8168-getrandbits.diff, 1.6 KB (added by , 16 years ago) |
---|
-
django/contrib/sessions/backends/base.py
2 2 import md5 3 3 import os 4 4 import random 5 import sys6 5 import time 7 6 from datetime import datetime, timedelta 8 7 try: … … 20 19 """ 21 20 TEST_COOKIE_NAME = 'testcookie' 22 21 TEST_COOKIE_VALUE = 'worked' 22 RANDOM_BITS = 64 23 23 24 24 def __init__(self, session_key=None): 25 25 self._session_key = session_key … … 111 111 "Returns session key that isn't being used." 112 112 # The random module is seeded when this Apache child is created. 113 113 # Use settings.SECRET_KEY as added salt. 114 # Using 63 bits wide RAND_MAX means collision probability at 115 # around 3,000,000,000 keys (that should be quite enough) 116 # due to the birthday paradox: 117 # int(sqrt((2 << 62) - 2)) == 3037000499 114 118 try: 115 119 pid = os.getpid() 116 120 except AttributeError: 117 121 # No getpid() in Jython, for example 118 122 pid = 1 119 123 while 1: 120 session_key = md5.new("%s%s%s%s" % (random.randint(0, sys.maxint - 1), 121 pid, time.time(), settings.SECRET_KEY)).hexdigest() 124 session_key = md5.new("%s%s%s%s" % 125 (random.getrandbits(self.RANDOM_BITS), pid, 126 time.time(), settings.SECRET_KEY)).hexdigest() 122 127 if not self.exists(session_key): 123 128 break 124 129 return session_key