SVN: r25217 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Tue, 28 Sep 2004 12:03:25 -0400
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-09-28 12:03:11 -0400 (Tue, 28 Sep 2004)
New Revision: 25217

Modified:
   trunk/quixote/session.py
   trunk/quixote/util.py
Log:
Move randbytes() function to quixote.util module.


Modified: trunk/quixote/session.py
===================================================================
--- trunk/quixote/session.py	2004-09-28 15:34:31 UTC (rev 25216)
+++ trunk/quixote/session.py	2004-09-28 16:03:11 UTC (rev 25217)
@@ -21,43 +21,12 @@
 doc/session-mgmt.txt for information on session persistence.
 """
 
-__revision__ = "$Id$"
+from time import time, localtime, strftime
 
-import os
-import binascii
-from time import time, localtime, strftime, clock
-
 from quixote import get_publisher
 from quixote.errors import SessionError
+from quixote.util import randbytes
 
-if hasattr(os, 'urandom'):
-    # available in Python 2.4 and also works on win32
-    def randbytes(bytes):
-        """Return bits of random data as a hex string."""
-        return binascii.hexlify(os.urandom(bytes))
-
-elif os.path.exists('/dev/urandom'):
-    # /dev/urandom is just as good as /dev/random for cookies (assuming
-    # SHA-1 is secure) and it never blocks.
-    def randbytes(bytes):
-        """Return bits of random data as a hex string."""
-        return binascii.hexlify(open("/dev/urandom").read(bytes))
-
-else:
-    # this is much less secure than the above function
-    import sha
-    _randstate = sha.new(str(time() + clock()))
-    def randbytes(bytes):
-        """Return bits of random data as a long integer."""
-        global _randstate
-        s = ""
-        chars = 2*bytes
-        while len(s) < chars:
-            _randstate.update(str(time() + clock()))
-            s += _randstate.hexdigest()
-        return s[:chars]
-
-
 class SessionManager:
     """
     SessionManager acts as a dictionary of all sessions, mapping session

Modified: trunk/quixote/util.py
===================================================================
--- trunk/quixote/util.py	2004-09-28 15:34:31 UTC (rev 25216)
+++ trunk/quixote/util.py	2004-09-28 16:03:11 UTC (rev 25217)
@@ -15,15 +15,56 @@
 See doc/static-files.txt for examples of their use.
 """
 
-__revision__ = "$Id$"
-
-import sys, xmlrpclib
-import os, mimetypes, urllib
+import sys
+import os
+import time
+import binascii
+import mimetypes
+import urllib
+import xmlrpclib
+from cStringIO import StringIO
+from rfc822 import formatdate
 from quixote import errors, html
 from quixote.http_response import Stream
-from cStringIO import StringIO
-from rfc822 import formatdate
 
+if hasattr(os, 'urandom'):
+    # available in Python 2.4 and also works on win32
+    def randbytes(bytes):
+        """Return bits of random data as a hex string."""
+        return binascii.hexlify(os.urandom(bytes))
+
+elif os.path.exists('/dev/urandom'):
+    # /dev/urandom is just as good as /dev/random for cookies (assuming
+    # SHA-1 is secure) and it never blocks.
+    def randbytes(bytes):
+        """Return bits of random data as a hex string."""
+        return binascii.hexlify(open("/dev/urandom").read(bytes))
+
+else:
+    # this is much less secure than the above function
+    import sha
+    class _PRNG:
+        def __init__(self):
+            self.state = sha.new(str(time.time() + time.clock()))
+            self.count = 0
+
+        def _get_bytes(self):
+            self.state.update('%s %d' % (time.time() + time.clock(),
+                                         self.count))
+            self.count += 1
+            return self.state.hexdigest()
+
+        def randbytes(self, bytes):
+            """Return bits of random data as a hex string."""
+            s = ""
+            chars = 2*bytes
+            while len(s) < chars:
+                s += self._get_bytes()
+            return s[:chars]
+
+    randbytes = _PRNG().randbytes
+
+
 def xmlrpc (request, func):
     """xmlrpc(request:Request, func:callable) : string