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