|
| 1 | +"""HMAC (Keyed-Hashing for Message Authentication) Python module. |
| 2 | +
|
| 3 | +Implements the HMAC algorithm as described by RFC 2104. |
| 4 | +""" |
| 5 | + |
| 6 | +import string |
| 7 | + |
| 8 | +def _strxor(s1, s2): |
| 9 | + """Utility method. XOR the two strings s1 and s2 (must have same length). |
| 10 | + """ |
| 11 | + return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2)) |
| 12 | + |
| 13 | +class HMAC: |
| 14 | + """RFC2104 HMAC class. |
| 15 | +
|
| 16 | + This (mostly) supports the API for Cryptographic Hash Functions (PEP 247). |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, key, msg = None, digestmod = None): |
| 20 | + """Create a new HMAC object. |
| 21 | +
|
| 22 | + key: key for the keyed hash object. |
| 23 | + msg: Initial input for the hash, if provided. |
| 24 | + digestmod: A module supporting PEP 247. Defaults to the md5 module. |
| 25 | + """ |
| 26 | + if digestmod == None: |
| 27 | + import md5 |
| 28 | + digestmod = md5 |
| 29 | + |
| 30 | + self.outer = digestmod.new() |
| 31 | + self.inner = digestmod.new() |
| 32 | + |
| 33 | + blocksize = 64 |
| 34 | + ipad = "\x36" * blocksize |
| 35 | + opad = "\x5C" * blocksize |
| 36 | + |
| 37 | + if len(key) > blocksize: |
| 38 | + key = digestmod.new(key).digest() |
| 39 | + |
| 40 | + key = key + chr(0) * (blocksize - len(key)) |
| 41 | + self.outer.update(_strxor(key, opad)) |
| 42 | + self.inner.update(_strxor(key, ipad)) |
| 43 | + if (msg): |
| 44 | + self.update(msg) |
| 45 | + |
| 46 | +## def clear(self): |
| 47 | +## raise NotImplementedError, "clear() method not available in HMAC." |
| 48 | + |
| 49 | + def update(self, msg): |
| 50 | + """Update this hashing object with the string msg. |
| 51 | + """ |
| 52 | + self.inner.update(msg) |
| 53 | + |
| 54 | + def copy(self): |
| 55 | + """Return a separate copy of this hashing object. |
| 56 | +
|
| 57 | + An update to this copy won't affect the original object. |
| 58 | + """ |
| 59 | + return HMAC(self) |
| 60 | + |
| 61 | + def digest(self): |
| 62 | + """Return the hash value of this hashing object. |
| 63 | +
|
| 64 | + This returns a string containing 8-bit data. The object is |
| 65 | + not altered in any way by this function; you can continue |
| 66 | + updating the object after calling this function. |
| 67 | + """ |
| 68 | + h = self.outer.copy() |
| 69 | + h.update(self.inner.digest()) |
| 70 | + return h.digest() |
| 71 | + |
| 72 | + def hexdigest(self): |
| 73 | + """Like digest(), but returns a string of hexadecimal digits instead. |
| 74 | + """ |
| 75 | + return "".join([string.zfill(hex(ord(x))[2:], 2) |
| 76 | + for x in tuple(self.digest())]) |
| 77 | + |
| 78 | +def new(key, msg = None, digestmod = None): |
| 79 | + """Create a new hashing object and return it. |
| 80 | +
|
| 81 | + key: The starting key for the hash. |
| 82 | + msg: if available, will immediately be hashed into the object's starting |
| 83 | + state. |
| 84 | +
|
| 85 | + You can now feed arbitrary strings into the object using its update() |
| 86 | + method, and can ask for the hash value at any time by calling its digest() |
| 87 | + method. |
| 88 | + """ |
| 89 | + return HMAC(key, msg, digestmod) |
| 90 | + |
| 91 | +def test(): |
| 92 | + def md5test(key, data, digest): |
| 93 | + h = HMAC(key, data) |
| 94 | + assert(h.hexdigest().upper() == digest.upper()) |
| 95 | + |
| 96 | + # Test vectors from the RFC |
| 97 | + md5test(chr(0x0b) * 16, |
| 98 | + "Hi There", |
| 99 | + "9294727A3638BB1C13F48EF8158BFC9D") |
| 100 | + |
| 101 | + md5test("Jefe", |
| 102 | + "what do ya want for nothing?", |
| 103 | + "750c783e6ab0b503eaa86e310a5db738") |
| 104 | + |
| 105 | + md5test(chr(0xAA)*16, |
| 106 | + chr(0xDD)*50, |
| 107 | + "56be34521d144c88dbb8c733f0e8b3f6") |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + test() |
0 commit comments