[pypy-svn] r56448 - in pypy/dist: lib-python pypy/lib

hpk at codespeak.net hpk at codespeak.net
Fri Jul 11 15:16:13 CEST 2008


Author: hpk
Date: Fri Jul 11 15:16:12 2008
New Revision: 56448

Added:
   pypy/dist/pypy/lib/hashlib.py   (contents, props changed)
Modified:
   pypy/dist/lib-python/conftest.py
Log:
add a stripped down hashlib module that supports md5 and sha1, with stripped down tests. 


Modified: pypy/dist/lib-python/conftest.py
==============================================================================
--- pypy/dist/lib-python/conftest.py	(original)
+++ pypy/dist/lib-python/conftest.py	Fri Jul 11 15:16:12 2008
@@ -523,6 +523,9 @@
 
     RegrTest('test_gzip.py', enabled=False, dumbtest=1),
     RegrTest('test_hash.py', enabled=True, core=True),
+    RegrTest('test_hashlib.py', enabled=True, core=True),
+        # test_hashlib is stripped down, originates from 2.5 
+    
     RegrTest('test_heapq.py', enabled=True, core=True),
     RegrTest('test_hexoct.py', enabled=True, core=True),
     RegrTest('test_hmac.py', enabled=True),

Added: pypy/dist/pypy/lib/hashlib.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/hashlib.py	Fri Jul 11 15:16:12 2008
@@ -0,0 +1,127 @@
+# XXX ported version of the original hashlib.py from python
+# XXX 2.5, only supports md5 and sha1 by using the existing 
+# XXX modules of PyPy 
+# $Id: hashlib.py 52533 2006-10-29 18:01:12Z georg.brandl $
+#
+#  Copyright (C) 2005   Gregory P. Smith (greg at electricrain.com)
+#  Licensed to PSF under a Contributor Agreement.
+#
+
+__doc__ = """hashlib module - A common interface to many hash functions.
+
+new(name, string='') - returns a new hash object implementing the
+                       given hash function; initializing the hash
+                       using the given string data.
+
+Named constructor functions are also available, these are much faster
+than using new():
+
+md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
+
+More algorithms may be available on your platform but the above are
+guaranteed to exist.
+
+Choose your hash function wisely.  Some have known collision weaknesses.
+sha384 and sha512 will be slow on 32 bit platforms.
+
+Hash objects have these methods:
+ - update(arg): Update the hash object with the string arg. Repeated calls
+                are equivalent to a single call with the concatenation of all
+                the arguments.
+ - digest():    Return the digest of the strings passed to the update() method
+                so far. This may contain non-ASCII characters, including
+                NUL bytes.
+ - hexdigest(): Like digest() except the digest is returned as a string of
+                double length, containing only hexadecimal digits.
+ - copy():      Return a copy (clone) of the hash object. This can be used to
+                efficiently compute the digests of strings that share a common
+                initial substring.
+
+For example, to obtain the digest of the string 'Nobody inspects the
+spammish repetition':
+
+    >>> import hashlib
+    >>> m = hashlib.md5()
+    >>> m.update("Nobody inspects")
+    >>> m.update(" the spammish repetition")
+    >>> m.digest()
+    '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
+
+More condensed:
+
+    >>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
+    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
+
+"""
+
+
+def __get_builtin_constructor(name):
+    if name in ('SHA1', 'sha1'):
+        import sha
+        return sha.new
+    elif name in ('MD5', 'md5'):
+        import md5
+        return md5.new
+    # XXX code for other codecs deleted here 
+    raise ValueError, "unsupported hash type"
+
+
+def __py_new(name, string=''):
+    """new(name, string='') - Return a new hashing object using the named algorithm;
+    optionally initialized with a string.
+    """
+    return __get_builtin_constructor(name)(string)
+
+
+def __hash_new(name, string=''):
+    """new(name, string='') - Return a new hashing object using the named algorithm;
+    optionally initialized with a string.
+    """
+    try:
+        return _hashlib.new(name, string)
+    except ValueError:
+        # If the _hashlib module (OpenSSL) doesn't support the named
+        # hash, try using our builtin implementations.
+        # This allows for SHA224/256 and SHA384/512 support even though
+        # the OpenSSL library prior to 0.9.8 doesn't provide them.
+        return __get_builtin_constructor(name)(string)
+
+
+try:
+    import _hashlib
+    # use the wrapper of the C implementation
+    new = __hash_new
+
+    for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)):
+        funcName = opensslFuncName[len('openssl_'):]
+        try:
+            # try them all, some may not work due to the OpenSSL
+            # version not supporting that algorithm.
+            f = getattr(_hashlib, opensslFuncName)
+            f()
+            # Use the C function directly (very fast)
+            exec funcName + ' = f'
+        except ValueError:
+            try:
+                # Use the builtin implementation directly (fast)
+                exec funcName + ' = __get_builtin_constructor(funcName)'
+            except ValueError:
+                # this one has no builtin implementation, don't define it
+                pass
+    # clean up our locals
+    del f
+    del opensslFuncName
+    del funcName
+
+except ImportError:
+    # We don't have the _hashlib OpenSSL module?
+    # use the built in legacy interfaces via a wrapper function
+    new = __py_new
+
+    # lookup the C function to use directly for the named constructors
+    md5 = __get_builtin_constructor('md5')
+    sha1 = __get_builtin_constructor('sha1')
+    #sha224 = __get_builtin_constructor('sha224')
+    #sha256 = __get_builtin_constructor('sha256')
+    #sha384 = __get_builtin_constructor('sha384')
+    #sha512 = __get_builtin_constructor('sha512')



More information about the Pypy-commit mailing list