--- hmac.py 2006-10-06 04:34:16.000000000 -0400 +++ /home/bmaurer/hm/myhmac1.py 2006-12-18 18:09:37.000000000 -0500 @@ -3,11 +3,14 @@ Implements the HMAC algorithm as described by RFC 2104. """ -def _strxor(s1, s2): +trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange (256)]) +trans_36 = "".join ([chr (x ^ 0x36) for x in xrange (256)]) + +def _strxor(s1, table): """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)) - + return s1.translate (table) + # The size of the digests returned by HMAC depends on the underlying # hashing module used. digest_size = None @@ -44,15 +47,14 @@ self.digest_size = digestmod.digest_size blocksize = 64 - ipad = "\x36" * blocksize - opad = "\x5C" * blocksize + if len(key) > blocksize: key = digestmod.new(key).digest() key = key + chr(0) * (blocksize - len(key)) - self.outer.update(_strxor(key, opad)) - self.inner.update(_strxor(key, ipad)) + self.outer.update(_strxor(key, trans_5C)) + self.inner.update(_strxor(key, trans_36)) if msg is not None: self.update(msg)