[pypy-svn] r64357 - in pypy/trunk/pypy/lib: . app_test
arigo at codespeak.net
arigo at codespeak.net
Sun Apr 19 13:32:29 CEST 2009
Author: arigo
Date: Sun Apr 19 13:32:28 2009
New Revision: 64357
Modified:
pypy/trunk/pypy/lib/_sha256.py
pypy/trunk/pypy/lib/_sha512.py
pypy/trunk/pypy/lib/app_test/test_hashlib.py
pypy/trunk/pypy/lib/app_test/test_md5_extra.py
pypy/trunk/pypy/lib/app_test/test_sha_extra.py
pypy/trunk/pypy/lib/md5.py
pypy/trunk/pypy/lib/sha.py
Log:
Add digestsize and digest_size a bit everywhere.
Modified: pypy/trunk/pypy/lib/_sha256.py
==============================================================================
--- pypy/trunk/pypy/lib/_sha256.py (original)
+++ pypy/trunk/pypy/lib/_sha256.py Sun Apr 19 13:32:28 2009
@@ -205,6 +205,8 @@
return ''.join([chr(i) for i in dig])
class sha256:
+ digest_size = digestsize = 32
+
def __init__(self, s=None):
self._sha = sha_init()
if s:
@@ -220,6 +222,8 @@
return ''.join(['%.2x' % ord(i) for i in self.digest()])
class sha224(sha256):
+ digest_size = digestsize = 28
+
def __init__(self, s=None):
self._sha = sha224_init()
if s:
Modified: pypy/trunk/pypy/lib/_sha512.py
==============================================================================
--- pypy/trunk/pypy/lib/_sha512.py (original)
+++ pypy/trunk/pypy/lib/_sha512.py Sun Apr 19 13:32:28 2009
@@ -235,6 +235,8 @@
return ''.join([chr(i) for i in dig])
class sha512:
+ digest_size = digestsize = 64
+
def __init__(self, s=None):
self._sha = sha_init()
if s:
@@ -250,6 +252,8 @@
return ''.join(['%.2x' % ord(i) for i in self.digest()])
class sha384(sha512):
+ digest_size = digestsize = 48
+
def __init__(self, s=None):
self._sha = sha384_init()
if s:
Modified: pypy/trunk/pypy/lib/app_test/test_hashlib.py
==============================================================================
--- pypy/trunk/pypy/lib/app_test/test_hashlib.py (original)
+++ pypy/trunk/pypy/lib/app_test/test_hashlib.py Sun Apr 19 13:32:28 2009
@@ -2,3 +2,20 @@
def test_unicode():
assert isinstance(hashlib.new('sha1', u'xxx'), _hashlib.hash)
+
+def test_attributes():
+ for name, expected_size in {'md5': 16,
+ 'sha1': 20,
+ 'sha224': 28,
+ 'sha256': 32,
+ 'sha384': 48,
+ 'sha512': 64,
+ }.items():
+ h = hashlib.new(name)
+ assert h.digest_size == expected_size
+ assert h.digestsize == expected_size
+
+ # also test the pure Python implementation
+ h = hashlib.__get_builtin_constructor(name)('')
+ assert h.digest_size == expected_size
+ assert h.digestsize == expected_size
Modified: pypy/trunk/pypy/lib/app_test/test_md5_extra.py
==============================================================================
--- pypy/trunk/pypy/lib/app_test/test_md5_extra.py (original)
+++ pypy/trunk/pypy/lib/app_test/test_md5_extra.py Sun Apr 19 13:32:28 2009
@@ -217,3 +217,11 @@
d2 = m2c.hexdigest()
assert d1 == d2
+
+
+def test_attributes():
+ assert pymd5.digest_size == 16
+ assert pymd5.digestsize == 16
+ assert pymd5.blocksize == 1
+ assert pymd5.md5().digest_size == 16
+ assert pymd5.md5().digestsize == 16
Modified: pypy/trunk/pypy/lib/app_test/test_sha_extra.py
==============================================================================
--- pypy/trunk/pypy/lib/app_test/test_sha_extra.py (original)
+++ pypy/trunk/pypy/lib/app_test/test_sha_extra.py Sun Apr 19 13:32:28 2009
@@ -22,3 +22,11 @@
def disabled_too_slow_test_case_3(self):
self.check("a" * 1000000,
"34aa973cd4c4daa4f61eeb2bdbad27316534016f")
+
+
+def test_attributes():
+ assert sha.digest_size == 20
+ assert sha.digestsize == 20
+ assert sha.blocksize == 1
+ assert sha.sha().digest_size == 20
+ assert sha.sha().digestsize == 20
Modified: pypy/trunk/pypy/lib/md5.py
==============================================================================
--- pypy/trunk/pypy/lib/md5.py (original)
+++ pypy/trunk/pypy/lib/md5.py Sun Apr 19 13:32:28 2009
@@ -115,6 +115,8 @@
class MD5Type:
"An implementation of the MD5 hash function in pure Python."
+ digest_size = digestsize = 16
+
def __init__(self):
"Initialisation."
@@ -370,7 +372,8 @@
# for consistency with the md5 module of the standard library.
# ======================================================================
-digest_size = 16
+digest_size = digestsize = 16
+blocksize = 1
def new(arg=None):
"""Return a new md5 crypto object.
Modified: pypy/trunk/pypy/lib/sha.py
==============================================================================
--- pypy/trunk/pypy/lib/sha.py (original)
+++ pypy/trunk/pypy/lib/sha.py Sun Apr 19 13:32:28 2009
@@ -117,6 +117,8 @@
class sha:
"An implementation of the MD5 hash function in pure Python."
+ digest_size = digestsize = 20
+
def __init__(self):
"Initialisation."
More information about the Pypy-commit
mailing list