Ticket #1180: use_63bit_random.diff
File use_63bit_random.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 from django.conf import settings … … 19 18 """ 20 19 TEST_COOKIE_NAME = 'testcookie' 21 20 TEST_COOKIE_VALUE = 'worked' 21 RAND_MAX = (2 << 62) - 2 22 22 23 23 def __init__(self, session_key=None): 24 24 self._session_key = session_key … … 110 114 "Returns session key that isn't being used." 111 115 # The random module is seeded when this Apache child is created. 112 116 # Use settings.SECRET_KEY as added salt. 117 # Using 63 bits wide RAND_MAX means collision probability at 118 # around 3,000,000,000 keys (that should be quite enough) 119 # due to the birthday paradox: 120 # int(sqrt((2 << 62) - 2)) == 3037000499 113 121 try: 114 122 pid = os.getpid() 115 123 except AttributeError: 116 124 # No getpid() in Jython, for example 117 125 pid = 1 118 126 while 1: 119 session_key = md5.new("%s%s%s%s" % (random.randint(0, sys.maxint - 1), 120 pid, time.time(), settings.SECRET_KEY)).hexdigest() 127 session_key = md5.new("%s%s%s%s" % 128 (random.randint(0, self.RAND_MAX), pid, 129 time.time(), settings.SECRET_KEY)).hexdigest() 121 130 if not self.exists(session_key): 122 131 break 123 132 return session_key