Commits

Amaury Forgeot d'Arc  committed a70edbd

hash.update() accepts arrays and other objects with the buffer interface.
CPython uses the s* code, try to emulate this in Python code.

  • Participants
  • Parent commits eb8f727

Comments (0)

Files changed (2)

File lib_pypy/_sha256.py

     sha_info['digestsize'] = 28
     return sha_info
 
+def getbuf(s):
+    if isinstance(s, str):
+        return s
+    elif isinstance(s, unicode):
+        return str(s)
+    else:
+        return buffer(s)
+
 def sha_update(sha_info, buffer):
     count = len(buffer)
     buffer_idx = 0
     def __init__(self, s=None):
         self._sha = sha_init()
         if s:
-            sha_update(self._sha, s)
+            sha_update(self._sha, getbuf(s))
     
     def update(self, s):
-        sha_update(self._sha, s)
+        sha_update(self._sha, getbuf(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, s)
+            sha_update(self._sha, getbuf(s))
 
     def copy(self):
         new = sha224.__new__(sha224)

File lib_pypy/_sha512.py

     sha_info['digestsize'] = 48
     return sha_info
 
+def getbuf(s):
+    if isinstance(s, str):
+        return s
+    elif isinstance(s, unicode):
+        return str(s)
+    else:
+        return buffer(s)
+
 def sha_update(sha_info, buffer):
     count = len(buffer)
     buffer_idx = 0
     def __init__(self, s=None):
         self._sha = sha_init()
         if s:
-            sha_update(self._sha, s)
+            sha_update(self._sha, getbuf(s))
     
     def update(self, s):
-        sha_update(self._sha, s)
+        sha_update(self._sha, getbuf(s))
     
     def digest(self):
         return sha_final(self._sha.copy())[:self._sha['digestsize']]
     def __init__(self, s=None):
         self._sha = sha384_init()
         if s:
-            sha_update(self._sha, s)
+            sha_update(self._sha, getbuf(s))
 
     def copy(self):
         new = sha384.__new__(sha384)