[pypy-commit] pypy py3k: Fix _sha modules until test_hashlib passes.
amauryfa
noreply at buildbot.pypy.org
Thu Nov 15 00:06:43 CET 2012
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r58921:53976f5997ee
Date: 2012-11-14 23:58 +0100
http://bitbucket.org/pypy/pypy/changeset/53976f5997ee/
Log: Fix _sha modules until test_hashlib passes.
diff --git a/lib_pypy/_sha1.py b/lib_pypy/_sha1.py
--- a/lib_pypy/_sha1.py
+++ b/lib_pypy/_sha1.py
@@ -230,6 +230,9 @@
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.
diff --git a/lib_pypy/_sha256.py b/lib_pypy/_sha256.py
--- a/lib_pypy/_sha256.py
+++ b/lib_pypy/_sha256.py
@@ -1,5 +1,3 @@
-import struct
-
SHA_BLOCKSIZE = 64
SHA_DIGESTSIZE = 32
@@ -131,15 +129,9 @@
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
@@ -155,8 +147,7 @@
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
@@ -170,7 +161,7 @@
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)
@@ -178,7 +169,7 @@
# 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):
@@ -219,10 +210,10 @@
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']]
@@ -241,7 +232,7 @@
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)
diff --git a/lib_pypy/_sha512.py b/lib_pypy/_sha512.py
--- a/lib_pypy/_sha512.py
+++ b/lib_pypy/_sha512.py
@@ -2,8 +2,6 @@
This code was Ported from CPython's sha512module.c
"""
-import struct
-
SHA_BLOCKSIZE = 128
SHA_DIGESTSIZE = 64
@@ -153,6 +151,8 @@
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
@@ -168,8 +168,7 @@
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
@@ -183,7 +182,7 @@
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)
@@ -250,7 +249,7 @@
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)
More information about the pypy-commit
mailing list