[Zodb-checkins] SVN: ZODB/branches/matt-python-versions/src/Z - Changed md5 and sha1 to use hashlib

matt@zope.com cvs-admin at zope.org
Tue Nov 18 17:02:57 EST 2008


Log message for revision 93105:
  - Changed md5 and sha1 to use hashlib
  - Created property to allow BaseException to use 'message' in Python 2.6
  

Changed:
  U   ZODB/branches/matt-python-versions/src/ZEO/auth/auth_digest.py
  U   ZODB/branches/matt-python-versions/src/ZEO/auth/base.py
  A   ZODB/branches/matt-python-versions/src/ZEO/hash.py
  U   ZODB/branches/matt-python-versions/src/ZEO/tests/auth_plaintext.py
  U   ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py
  U   ZODB/branches/matt-python-versions/src/ZODB/POSException.py
  U   ZODB/branches/matt-python-versions/src/ZODB/scripts/fstail.py

-=-
Modified: ZODB/branches/matt-python-versions/src/ZEO/auth/auth_digest.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/auth/auth_digest.py	2008-11-18 21:23:31 UTC (rev 93104)
+++ ZODB/branches/matt-python-versions/src/ZEO/auth/auth_digest.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -37,13 +37,13 @@
 
 import os
 import random
-import sha
 import struct
 import time
 
+from ZEO.Exceptions import AuthError
+from ZEO.StorageServer import ZEOStorage
 from ZEO.auth.base import Database, Client
-from ZEO.StorageServer import ZEOStorage
-from ZEO.Exceptions import AuthError
+from ZEO.hash import sha1
 
 def get_random_bytes(n=8):
     if os.path.exists("/dev/urandom"):
@@ -56,7 +56,7 @@
     return s
 
 def hexdigest(s):
-    return sha.new(s).hexdigest()
+    return sha1(s).hexdigest()
 
 class DigestDatabase(Database):
     def __init__(self, filename, realm=None):
@@ -76,7 +76,7 @@
     # HMAC wants a 64-byte key.  We don't want to use h_up
     # directly because it would never change over time.  Instead
     # use the hash plus part of h_up.
-    return sha.new("%s:%s" % (h_up, nonce)).digest() + h_up[:44]
+    return sha1("%s:%s" % (h_up, nonce)).digest() + h_up[:44]
 
 class StorageClass(ZEOStorage):
     def set_database(self, database):
@@ -92,7 +92,7 @@
     def _get_nonce(self):
         # RFC 2069 recommends a nonce of the form
         # H(client-IP ":" time-stamp ":" private-key)
-        dig = sha.sha()
+        dig = sha1()
         dig.update(str(self.connection.addr))
         dig.update(self._get_time())
         dig.update(self.noncekey)

Modified: ZODB/branches/matt-python-versions/src/ZEO/auth/base.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/auth/base.py	2008-11-18 21:23:31 UTC (rev 93104)
+++ ZODB/branches/matt-python-versions/src/ZEO/auth/base.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -18,8 +18,9 @@
 """
 
 import os
-import sha
 
+from ZEO.hash import sha1
+
 class Client:
     # Subclass should override to list the names of methods that
     # will be called on the server.
@@ -97,11 +98,11 @@
             self.realm = line[len("realm "):]
 
         for line in L:
-            username, hash = line.strip().split(":", 1)
-            self._users[username] = hash.strip()
+            username, hashvar = line.strip().split(":", 1)
+            self._users[username] = hashvar.strip()
 
     def _store_password(self, username, password):
-        self._users[username] = self.hash(password)
+        self._users[username] = self.hash_func(password)
 
     def get_password(self, username):
         """Returns password hash for specified username.
@@ -112,8 +113,8 @@
             raise LookupError("No such user: %s" % username)
         return self._users[username]
 
-    def hash(self, s):
-        return sha.new(s).hexdigest()
+    def hash_func(self, s):
+        return sha1(s).hexdigest()
 
     def add_user(self, username, password):
         if self._users.has_key(username):

Copied: ZODB/branches/matt-python-versions/src/ZEO/hash.py (from rev 93051, ZODB/branches/matt-python2.6/src/ZEO/hash.py)
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/hash.py	                        (rev 0)
+++ ZODB/branches/matt-python-versions/src/ZEO/hash.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+"""In Python 2.6, the "sha" and "md5" modules have been deprecated
+in favor of using hashlib for both. This class allows for compatibility
+between versions."""
+
+import sys
+
+if sys.version_info[:2] >= (2, 6):
+    import hashlib
+    sha1 = hashlib.sha1
+else:
+    import sha
+    sha1 = sha.new

Modified: ZODB/branches/matt-python-versions/src/ZEO/tests/auth_plaintext.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/tests/auth_plaintext.py	2008-11-18 21:23:31 UTC (rev 93104)
+++ ZODB/branches/matt-python-versions/src/ZEO/tests/auth_plaintext.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -19,14 +19,13 @@
 is provided by not storing plaintext passwords on disk.
 """
 
-import sha
-
 from ZEO.StorageServer import ZEOStorage
 from ZEO.auth import register_module
 from ZEO.auth.base import Client, Database
+from ZEO.hash import sha1
 
 def session_key(username, realm, password):
-    return sha.new("%s:%s:%s" % (username, realm, password)).hexdigest()
+    return sha1("%s:%s:%s" % (username, realm, password)).hexdigest()
 
 class StorageClass(ZEOStorage):
 
@@ -36,7 +35,7 @@
         except LookupError:
             return 0
 
-        password_dig = sha.new(password).hexdigest()
+        password_dig = sha1(password).hexdigest()
         if dbpw == password_dig:
             self.connection.setSessionKey(session_key(username,
                                                       self.database.realm,

Modified: ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py	2008-11-18 21:23:31 UTC (rev 93104)
+++ ZODB/branches/matt-python-versions/src/ZEO/zrpc/smac.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -31,7 +31,6 @@
     import hmac
 except ImportError:
     import _hmac as hmac
-import sha
 import socket
 import struct
 import threading
@@ -40,8 +39,9 @@
 
 from ZODB.loglevels import TRACE
 
+from ZEO.hash import sha1
+from ZEO.zrpc.error import DisconnectedError
 from ZEO.zrpc.log import log, short_repr
-from ZEO.zrpc.error import DisconnectedError
 
 
 # Use the dictionary to make sure we get the minimum number of errno
@@ -147,8 +147,8 @@
         # and thus iterator, because it contains a yield statement.
 
         def hack():
-            self.__hmac_send = hmac.HMAC(sesskey, digestmod=sha)
-            self.__hmac_recv = hmac.HMAC(sesskey, digestmod=sha)
+            self.__hmac_send = hmac.HMAC(sesskey, digestmod=sha1)
+            self.__hmac_recv = hmac.HMAC(sesskey, digestmod=sha1)
             if False:
                 yield ''
 

Modified: ZODB/branches/matt-python-versions/src/ZODB/POSException.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZODB/POSException.py	2008-11-18 21:23:31 UTC (rev 93104)
+++ ZODB/branches/matt-python-versions/src/ZODB/POSException.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -34,22 +34,35 @@
 class POSError(StandardError):
     """Persistent object system error."""
 
+    # The 'message' attribute was deprecated for BaseException with
+    # Python 2.6; here we create descriptor properties to continue using it
+    def __set_message(self, v):
+        self.__dict__['message'] = v
+
+    def __get_message(self):
+        return self.__dict__['message']
+
+    def __del_message(self):
+        del self.__dict__['message']
+
+    message = property(__get_message, __set_message, __del_message)
+
     def __reduce__(self):
-        # Cope extra data from internal structures
+        # Copy extra data from internal structures
         state = self.__dict__.copy()
-        state['message'] = self.message
+        #state['message'] = self.message
         state['args'] = self.args
 
         return (_recon, (self.__class__, state))
 
-class POSKeyError(KeyError, POSError):
+class POSKeyError(POSError, KeyError):
     """Key not found in database."""
 
     def __str__(self):
         return oid_repr(self.args[0])
 
 
-class ConflictError(TransactionError):
+class ConflictError(POSError, TransactionError):
     """Two transactions tried to modify the same object at once.
 
     This transaction should be resubmitted.
@@ -213,7 +226,7 @@
         return "BTrees conflict error at %d/%d/%d: %s" % (
             self.p1, self.p2, self.p3, self.msgs[self.reason])
 
-class DanglingReferenceError(TransactionError):
+class DanglingReferenceError(POSError, TransactionError):
     """An object has a persistent reference to a missing object.
 
     If an object is stored and it has a reference to another object

Modified: ZODB/branches/matt-python-versions/src/ZODB/scripts/fstail.py
===================================================================
--- ZODB/branches/matt-python-versions/src/ZODB/scripts/fstail.py	2008-11-18 21:23:31 UTC (rev 93104)
+++ ZODB/branches/matt-python-versions/src/ZODB/scripts/fstail.py	2008-11-18 22:02:57 UTC (rev 93105)
@@ -17,9 +17,10 @@
 
 from ZODB.fstools import prev_txn
 
+from ZEO.hash import sha1
+
 import binascii
 import getopt
-import sha
 import sys
 
 def main(path, ntxn):
@@ -28,7 +29,7 @@
     th = prev_txn(f)
     i = ntxn
     while th and i > 0:
-        hash = sha.sha(th.get_raw_data()).digest()
+        hash = sha1(th.get_raw_data()).digest()
         l = len(str(th.get_timestamp())) + 1
         th.read_meta()
         print "%s: hash=%s" % (th.get_timestamp(),



More information about the Zodb-checkins mailing list