[pypy-svn] r60487 - pypy/trunk/pypy/lib

hruske at codespeak.net hruske at codespeak.net
Sat Dec 13 11:07:21 CET 2008


Author: hruske
Date: Sat Dec 13 11:07:19 2008
New Revision: 60487

Modified:
   pypy/trunk/pypy/lib/_sha256.py
   pypy/trunk/pypy/lib/hashlib.py
Log:
Add sha224 support.


Modified: pypy/trunk/pypy/lib/_sha256.py
==============================================================================
--- pypy/trunk/pypy/lib/_sha256.py	(original)
+++ pypy/trunk/pypy/lib/_sha256.py	Sat Dec 13 11:07:19 2008
@@ -122,7 +122,8 @@
 	sha_info['digestsize'] = 32
 	return sha_info
 
-def sha224_init(sha_info):
+def sha224_init():
+	sha_info = new_shaobject()
 	sha_info['digest'] = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]
 	sha_info['count_lo'] = 0
 	sha_info['count_hi'] = 0
@@ -204,7 +205,6 @@
 	return ''.join([chr(i) for i in dig])
 
 class sha256:
-	
 	def __init__(self, s=None):
 		self._sha = sha_init()
 		if s:
@@ -214,11 +214,17 @@
 		sha_update(self._sha, s)
 	
 	def digest(self):
-		return sha_final(self._sha.copy())
+		return sha_final(self._sha.copy())[:self._sha['digestsize']]
 	
 	def hexdigest(self):
 		return ''.join(['%.2x' % ord(i) for i in self.digest()])
 
+class sha224(sha256):
+	def __init__(self, s=None):
+		self._sha = sha224_init()
+		if s:
+			sha_update(self._sha, s)
+
 def test():
 	a_str = "just a test string"
 	

Modified: pypy/trunk/pypy/lib/hashlib.py
==============================================================================
--- pypy/trunk/pypy/lib/hashlib.py	(original)
+++ pypy/trunk/pypy/lib/hashlib.py	Sat Dec 13 11:07:19 2008
@@ -63,6 +63,9 @@
     elif name in ('SHA256', 'sha256'):
         import _sha256
         return _sha256.sha256
+    elif name in ('SHA224', 'sha224'):
+        import _sha256
+        return _sha256.sha224
     raise ValueError, "unsupported hash type"
 
 def __hash_new(name, string=''):



More information about the Pypy-commit mailing list