Amaury Forgeot d'Arc avatar Amaury Forgeot d'Arc committed 53976f5

Fix _sha modules until test_hashlib passes.

Comments (0)

Files changed (3)

lib_pypy/_sha1.py

         to the hashed string.
         """
 
+        if isinstance(inBuf, str):
+            raise TypeError("Unicode strings must be encoded before hashing")
+
         leninBuf = len(inBuf)
 
         # Compute number of bytes mod 64.

lib_pypy/_sha256.py

-import struct
-
 SHA_BLOCKSIZE = 64
 SHA_DIGESTSIZE = 32
 
     sha_info['digestsize'] = 28
     return sha_info
 
-def getbuf(s):
-    if isinstance(s, str):
-        return s
-    elif isinstance(s, str):
-        return str(s)
-    else:
-        return buffer(s)
-
 def sha_update(sha_info, buffer):
+    if isinstance(buffer, str):
+        raise TypeError("Unicode strings must be encoded before hashing")
     count = len(buffer)
     buffer_idx = 0
     clo = (sha_info['count_lo'] + (count << 3)) & 0xffffffff
             i = count
         
         # copy buffer
-        for x in enumerate(buffer[buffer_idx:buffer_idx+i]):
-            sha_info['data'][sha_info['local']+x[0]] = struct.unpack('B', x[1])[0]
+        sha_info['data'][sha_info['local']:sha_info['local']+i] = buffer[buffer_idx:buffer_idx+i]
         
         count -= i
         buffer_idx += i
     
     while count >= SHA_BLOCKSIZE:
         # copy buffer
-        sha_info['data'] = [struct.unpack('B',c)[0] for c in buffer[buffer_idx:buffer_idx + SHA_BLOCKSIZE]]
+        sha_info['data'] = list(buffer[buffer_idx:buffer_idx + SHA_BLOCKSIZE])
         count -= SHA_BLOCKSIZE
         buffer_idx += SHA_BLOCKSIZE
         sha_transform(sha_info)
     
     # copy buffer
     pos = sha_info['local']
-    sha_info['data'][pos:pos+count] = [struct.unpack('B',c)[0] for c in buffer[buffer_idx:buffer_idx + count]]
+    sha_info['data'][pos:pos+count] = buffer[buffer_idx:buffer_idx + count]
     sha_info['local'] = count
 
 def sha_final(sha_info):
     def __init__(self, s=None):
         self._sha = sha_init()
         if s:
-            sha_update(self._sha, getbuf(s))
+            sha_update(self._sha, s)
     
     def update(self, s):
-        sha_update(self._sha, getbuf(s))
+        sha_update(self._sha, s)
     
     def digest(self):
         return sha_final(self._sha.copy())[:self._sha['digestsize']]
     def __init__(self, s=None):
         self._sha = sha224_init()
         if s:
-            sha_update(self._sha, getbuf(s))
+            sha_update(self._sha, s)
 
     def copy(self):
         new = sha224.__new__(sha224)

lib_pypy/_sha512.py

 This code was Ported from CPython's sha512module.c
 """
 
-import struct
-
 SHA_BLOCKSIZE = 128
 SHA_DIGESTSIZE = 64
 
     return sha_info
 
 def sha_update(sha_info, buffer):
+    if isinstance(buffer, str):
+        raise TypeError("Unicode strings must be encoded before hashing")
     count = len(buffer)
     buffer_idx = 0
     clo = (sha_info['count_lo'] + (count << 3)) & 0xffffffff
             i = count
         
         # copy buffer
-        for x in enumerate(buffer[buffer_idx:buffer_idx+i]):
-            sha_info['data'][sha_info['local']+x[0]] = struct.unpack('B', x[1])[0]
+        sha_info['data'][sha_info['local']:sha_info['local']+i] = buffer[buffer_idx:buffer_idx+i]
         
         count -= i
         buffer_idx += i
     
     while count >= SHA_BLOCKSIZE:
         # copy buffer
-        sha_info['data'] = [struct.unpack('B',c)[0] for c in buffer[buffer_idx:buffer_idx + SHA_BLOCKSIZE]]
+        sha_info['data'] = list(buffer[buffer_idx:buffer_idx + SHA_BLOCKSIZE])
         count -= SHA_BLOCKSIZE
         buffer_idx += SHA_BLOCKSIZE
         sha_transform(sha_info)
         return sha_final(self._sha.copy())[:self._sha['digestsize']]
     
     def hexdigest(self):
-        return ''.join(['%.2x' % ord(i) for i in self.digest()])
+        return ''.join(['%.2x' % i for i in self.digest()])
 
     def copy(self):
         new = sha512.__new__(sha512)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.