quixote session.py,1.58,1.59

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]>
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Update of /home/cvs/quixote
In directory hewson:/tmp/cvs-serv10596

Modified Files:
	session.py 
Log Message:
Use binascii.hexlify instead of coding our own.  Use sha.hexdigest
instead of sha.digest.  Rename randlong() to randbytes() and remove
packbytes().  Thanks to Jon Dyte for the idea.


Index: session.py
===================================================================
RCS file: /home/cvs/quixote/session.py,v
retrieving revision 1.58
retrieving revision 1.59
diff -u -d -r1.58 -r1.59
--- session.py	18 Oct 2002 21:16:23 -0000	1.58
+++ session.py	25 Oct 2002 17:31:39 -0000	1.59
@@ -21,40 +21,33 @@
 
 __revision__ = "$Id$"
 
-import sys, string
+import sys, string, binascii
 from time import time, localtime, strftime, clock
 
 from quixote import get_publisher
 from quixote.errors import SessionError
 
-def packbytes(s):
-    "convert a string of bytes into a long integer"
-    n = 0L
-    for b in s:
-        n <<= 8
-        n |= ord(b)
-    return n
-    
 try:
     # /dev/urandom is just as good as /dev/random for cookies (assuming
     # SHA-1 is secure) and it never blocks.
     open("/dev/urandom")
-    def randlong(bytes):
-        """Return bits of random data as a long integer."""
-        return packbytes(open("/dev/urandom").read(bytes))
+    def randbytes(bytes):
+        """Return bits of random data as a hex string."""
+        return binascii.hexlify(open("/dev/urandom").read(bytes))
 
 except IOError:
     # this is much less secure than the above function
     import sha
     _randstate = sha.new(str(time() + clock()))
-    def randlong(bytes):
+    def randbytes(bytes):
         """Return bits of random data as a long integer."""
         global _randstate
         s = ""
-        while len(s) < bytes:
+        chars = 2*bytes
+        while len(s) < chars:
             _randstate.update(str(time() + clock()))
-            s += _randstate.digest()
-        return packbytes(s[:bytes])
+            s += _randstate.hexdigest()
+        return s[:chars]
 
 
 class SessionManager:
@@ -255,7 +248,7 @@
         # used with the session manager mapping interface.)
         id = None
         while id is None or self.has_session(id):
-            id = "%016X" % randlong(8)  # 64-bit random number
+            id = randbytes(8)  # 64-bit random number
         return id
 
     def _create_session (self, request):
@@ -578,7 +571,7 @@
         tokens for this session.  A maximum of MAX_FORM_TOKENS are saved.
         The new token is returned.
         """
-        token = "%016X" % randlong(8)
+        token = randbytes(8)
         self._form_tokens.append(token)
         extra = len(self._form_tokens) - self.MAX_FORM_TOKENS
         if extra > 0:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.