summaryrefslogtreecommitdiffstats
path: root/Lib/hmac.py
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2006-12-19 14:13:05 (GMT)
committerAndrew M. Kuchling <amk@amk.ca>2006-12-19 14:13:05 (GMT)
commit8fe2d2015dc57e4e048f733c7fbb5c0712b05d15 (patch)
treead30da20b8b887301ad86cdf7a58bd354609b3c2 /Lib/hmac.py
parente3a985fe9aaa55088cf3cb03244949c72f8aaed9 (diff)
downloadcpython-8fe2d2015dc57e4e048f733c7fbb5c0712b05d15.zip
cpython-8fe2d2015dc57e4e048f733c7fbb5c0712b05d15.tar.gz
cpython-8fe2d2015dc57e4e048f733c7fbb5c0712b05d15.tar.bz2
[Patch #1618455 by Ben Maurer] Improve speed of HMAC by using str.translate()
instead of a more general XOR that has to construct a list. Slightly modified from Maurer's patch: the _strxor() function is no longer necessary at all.
Diffstat (limited to 'Lib/hmac.py')
-rw-r--r--Lib/hmac.py13
1 files changed, 4 insertions, 9 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py
index 41d6c6c..df2bffd 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -3,10 +3,8 @@
Implements the HMAC algorithm as described by RFC 2104.
"""
-def _strxor(s1, s2):
- """Utility method. XOR the two strings s1 and s2 (must have same length).
- """
- return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
+trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
+trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
# The size of the digests returned by HMAC depends on the underlying
# hashing module used.
@@ -50,15 +48,12 @@ class HMAC:
self.digest_size = self.inner.digest_size
blocksize = 64
- ipad = "\x36" * blocksize
- opad = "\x5C" * blocksize
-
if len(key) > blocksize:
key = self.digest_cons(key).digest()
key = key + chr(0) * (blocksize - len(key))
- self.outer.update(_strxor(key, opad))
- self.inner.update(_strxor(key, ipad))
+ self.outer.update(key.translate(trans_5C))
+ self.inner.update(key.translate(trans_36))
if msg is not None:
self.update(msg)