[Zodb-checkins] SVN: ZODB/branches/matt-python-versions/src/ZEO/zrpc/ Added ZEO.hash call for digestmod reqs.

matt@zope.com cvs-admin at zope.org
Thu Nov 20 17:43:24 EST 2008


Log message for revision 93198:
  Added ZEO.hash call for digestmod reqs.
  

Changed:
  D   ZODB/branches/matt-python-versions/src/ZEO/zrpc/_hmac.py
  U   ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py

-=-
Deleted: ZODB/branches/matt-python-versions/src/ZEO/zrpc/_hmac.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/zrpc/_hmac.py	2008-11-20 22:41:05 UTC (rev 93197)
+++ ZODB/branches/matt-python-versions/src/ZEO/zrpc/_hmac.py	2008-11-20 22:43:24 UTC (rev 93198)
@@ -1,104 +0,0 @@
-# This file is a slightly modified copy of Python 2.3's Lib/hmac.py.
-# This file is under the Python Software Foundation (PSF) license.
-
-"""HMAC (Keyed-Hashing for Message Authentication) Python module.
-
-Implements the HMAC algorithm as described by RFC 2104.
-"""
-
-def _strxor(s1, s2):
-    """Utility method. XOR the two strings s1 and s2 (must have same length).
-    """
-    return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
-
-# The size of the digests returned by HMAC depends on the underlying
-# hashing module used.
-digest_size = None
-
-class HMAC:
-    """RFC2104 HMAC class.
-
-    This supports the API for Cryptographic Hash Functions (PEP 247).
-    """
-
-    def __init__(self, key, msg = None, digestmod = None):
-        """Create a new HMAC object.
-
-        key:       key for the keyed hash object.
-        msg:       Initial input for the hash, if provided.
-        digestmod: A module supporting PEP 247. Defaults to the md5 module.
-        """
-        if digestmod is None:
-            import md5
-            digestmod = md5
-
-        self.digestmod = digestmod
-        self.outer = digestmod.new()
-        self.inner = digestmod.new()
-        # Python 2.1 and 2.2 differ about the correct spelling
-        try:
-            self.digest_size = digestmod.digestsize
-        except AttributeError:
-            self.digest_size = digestmod.digest_size
-
-        blocksize = 64
-        ipad = "\x36" * blocksize
-        opad = "\x5C" * blocksize
-
-        if len(key) > blocksize:
-            key = digestmod.new(key).digest()
-
-        key = key + chr(0) * (blocksize - len(key))
-        self.outer.update(_strxor(key, opad))
-        self.inner.update(_strxor(key, ipad))
-        if msg is not None:
-            self.update(msg)
-
-##    def clear(self):
-##        raise NotImplementedError("clear() method not available in HMAC.")
-
-    def update(self, msg):
-        """Update this hashing object with the string msg.
-        """
-        self.inner.update(msg)
-
-    def copy(self):
-        """Return a separate copy of this hashing object.
-
-        An update to this copy won't affect the original object.
-        """
-        other = HMAC("")
-        other.digestmod = self.digestmod
-        other.inner = self.inner.copy()
-        other.outer = self.outer.copy()
-        return other
-
-    def digest(self):
-        """Return the hash value of this hashing object.
-
-        This returns a string containing 8-bit data.  The object is
-        not altered in any way by this function; you can continue
-        updating the object after calling this function.
-        """
-        h = self.outer.copy()
-        h.update(self.inner.digest())
-        return h.digest()
-
-    def hexdigest(self):
-        """Like digest(), but returns a string of hexadecimal digits instead.
-        """
-        return "".join([hex(ord(x))[2:].zfill(2)
-                        for x in tuple(self.digest())])
-
-def new(key, msg = None, digestmod = None):
-    """Create a new hashing object and return it.
-
-    key: The starting key for the hash.
-    msg: if available, will immediately be hashed into the object's starting
-    state.
-
-    You can now feed arbitrary strings into the object using its update()
-    method, and can ask for the hash value at any time by calling its digest()
-    method.
-    """
-    return HMAC(key, msg, digestmod)

Modified: ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py	2008-11-20 22:41:05 UTC (rev 93197)
+++ ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py	2008-11-20 22:43:24 UTC (rev 93198)
@@ -27,10 +27,7 @@
 
 import asyncore
 import errno
-try:
-    import hmac
-except ImportError:
-    import _hmac as hmac
+import hmac
 import socket
 import struct
 import threading
@@ -147,8 +144,8 @@
         # and thus iterator, because it contains a yield statement.
 
         def hack():
-            self.__hmac_send = hmac.HMAC(sesskey, digestmod=sha1)
-            self.__hmac_recv = hmac.HMAC(sesskey, digestmod=sha1)
+            self.__hmac_send = hmac.HMAC(sesskey, digestmod=ZEO.hash)
+            self.__hmac_recv = hmac.HMAC(sesskey, digestmod=ZEO.hash)
             if False:
                 yield ''
 



More information about the Zodb-checkins mailing list