[pypy-commit] pypy py3k: fixes in pure-Python implementation of sha1 and md5:
amauryfa
noreply at buildbot.pypy.org
Sat Jan 14 21:48:28 CET 2012
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r51319:5f39f911a130
Date: 2011-12-22 11:06 +0100
http://bitbucket.org/pypy/pypy/changeset/5f39f911a130/
Log: fixes in pure-Python implementation of sha1 and md5: they accept
bytes, not strings
diff --git a/lib_pypy/_md5.py b/lib_pypy/_md5.py
--- a/lib_pypy/_md5.py
+++ b/lib_pypy/_md5.py
@@ -47,16 +47,16 @@
def _bytelist2long(list):
"Transform a list of characters into a list of longs."
- imax = len(list)/4
+ imax = len(list) // 4
hl = [0L] * imax
j = 0
i = 0
while i < imax:
- b0 = long(ord(list[j]))
- b1 = (long(ord(list[j+1]))) << 8
- b2 = (long(ord(list[j+2]))) << 16
- b3 = (long(ord(list[j+3]))) << 24
+ b0 = list[j]
+ b1 = list[j+1] << 8
+ b2 = list[j+2] << 16
+ b3 = list[j+3] << 24
hl[i] = b0 | b1 |b2 | b3
i = i+1
j = j+4
@@ -118,7 +118,7 @@
digest_size = digestsize = 16
block_size = 64
- def __init__(self):
+ def __init__(self, arg=None):
"Initialisation."
# Initial message length in bits(!).
@@ -132,6 +132,9 @@
# to start from scratch on the same object.
self.init()
+ if arg:
+ self.update(arg)
+
def init(self):
"Initialize the message-digest and set all fields to zero."
@@ -316,7 +319,7 @@
else:
padLen = 120 - index
- padding = ['\200'] + ['\000'] * 63
+ padding = [0o200] + [0] * 63
self.update(padding[:padLen])
# Append length (before padding).
@@ -346,7 +349,7 @@
binary environments.
"""
- return ''.join(['%02x' % ord(c) for c in self.digest()])
+ return ''.join(['%02x' % c for c in self.digest()])
def copy(self):
"""Return a clone object.
diff --git a/lib_pypy/_sha1.py b/lib_pypy/_sha1.py
--- a/lib_pypy/_sha1.py
+++ b/lib_pypy/_sha1.py
@@ -35,7 +35,7 @@
"""
# After much testing, this algorithm was deemed to be the fastest.
- s = ''
+ s = b''
pack = struct.pack
while n > 0:
s = pack('>I', n & 0xffffffffL) + s
@@ -69,10 +69,10 @@
j = 0
i = 0
while i < imax:
- b0 = long(ord(list[j])) << 24
- b1 = long(ord(list[j+1])) << 16
- b2 = long(ord(list[j+2])) << 8
- b3 = long(ord(list[j+3]))
+ b0 = list[j] << 24
+ b1 = list[j+1] << 16
+ b2 = list[j+2] << 8
+ b3 = list[j+3]
hl[i] = b0 | b1 | b2 | b3
i = i+1
j = j+4
@@ -280,7 +280,7 @@
else:
padLen = 120 - index
- padding = ['\200'] + ['\000'] * 63
+ padding = [0o200] + [0] * 63
self.update(padding[:padLen])
# Append length (before padding).
@@ -314,7 +314,7 @@
used to exchange the value safely in email or other non-
binary environments.
"""
- return ''.join(['%02x' % ord(c) for c in self.digest()])
+ return ''.join(['%02x' % c for c in self.digest()])
def copy(self):
"""Return a clone object.
More information about the pypy-commit
mailing list