Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 1 | """HMAC (Keyed-Hashing for Message Authentication) Python module. |
| 2 | |
| 3 | Implements the HMAC algorithm as described by RFC 2104. |
| 4 | """ |
| 5 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 6 | def _strxor(s1, s2): |
| 7 | """Utility method. XOR the two strings s1 and s2 (must have same length). |
| 8 | """ |
| 9 | return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2)) |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 10 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 11 | # The size of the digests returned by HMAC depends on the underlying |
| 12 | # hashing module used. |
| 13 | digest_size = None |
| 14 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 15 | class HMAC: |
| 16 | """RFC2104 HMAC class. |
| 17 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 18 | This supports the API for Cryptographic Hash Functions (PEP 247). |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 19 | """ |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 20 | |
| 21 | def __init__(self, key, msg = None, digestmod = None): |
| 22 | """Create a new HMAC object. |
| 23 | |
| 24 | key: key for the keyed hash object. |
| 25 | msg: Initial input for the hash, if provided. |
| 26 | digestmod: A module supporting PEP 247. Defaults to the md5 module. |
| 27 | """ |
Raymond Hettinger | 7fdfc2d | 2002-05-31 17:49:10 +0000 | [diff] [blame] | 28 | if digestmod is None: |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 29 | import md5 |
| 30 | digestmod = md5 |
| 31 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 32 | self.digestmod = digestmod |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 33 | self.outer = digestmod.new() |
| 34 | self.inner = digestmod.new() |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 35 | self.digest_size = digestmod.digest_size |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 37 | blocksize = 64 |
| 38 | ipad = "\x36" * blocksize |
| 39 | opad = "\x5C" * blocksize |
| 40 | |
| 41 | if len(key) > blocksize: |
| 42 | key = digestmod.new(key).digest() |
| 43 | |
| 44 | key = key + chr(0) * (blocksize - len(key)) |
| 45 | self.outer.update(_strxor(key, opad)) |
| 46 | self.inner.update(_strxor(key, ipad)) |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 47 | if msg is not None: |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 48 | self.update(msg) |
| 49 | |
| 50 | ## def clear(self): |
| 51 | ## raise NotImplementedError, "clear() method not available in HMAC." |
| 52 | |
| 53 | def update(self, msg): |
| 54 | """Update this hashing object with the string msg. |
| 55 | """ |
| 56 | self.inner.update(msg) |
| 57 | |
| 58 | def copy(self): |
| 59 | """Return a separate copy of this hashing object. |
| 60 | |
| 61 | An update to this copy won't affect the original object. |
| 62 | """ |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 63 | other = HMAC("") |
| 64 | other.digestmod = self.digestmod |
| 65 | other.inner = self.inner.copy() |
| 66 | other.outer = self.outer.copy() |
| 67 | return other |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 68 | |
| 69 | def digest(self): |
| 70 | """Return the hash value of this hashing object. |
| 71 | |
| 72 | This returns a string containing 8-bit data. The object is |
| 73 | not altered in any way by this function; you can continue |
| 74 | updating the object after calling this function. |
| 75 | """ |
| 76 | h = self.outer.copy() |
| 77 | h.update(self.inner.digest()) |
| 78 | return h.digest() |
| 79 | |
| 80 | def hexdigest(self): |
| 81 | """Like digest(), but returns a string of hexadecimal digits instead. |
| 82 | """ |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 83 | return "".join([hex(ord(x))[2:].zfill(2) |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 84 | for x in tuple(self.digest())]) |
| 85 | |
| 86 | def new(key, msg = None, digestmod = None): |
| 87 | """Create a new hashing object and return it. |
| 88 | |
| 89 | key: The starting key for the hash. |
| 90 | msg: if available, will immediately be hashed into the object's starting |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 91 | state. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 92 | |
| 93 | You can now feed arbitrary strings into the object using its update() |
| 94 | method, and can ask for the hash value at any time by calling its digest() |
| 95 | method. |
| 96 | """ |
| 97 | return HMAC(key, msg, digestmod) |