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 | |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 15 | # A unique object passed by HMAC.copy() to the HMAC constructor, in order |
| 16 | # that the latter return very quickly. HMAC("") in contrast is quite |
| 17 | # expensive. |
| 18 | _secret_backdoor_key = [] |
| 19 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 20 | class HMAC: |
| 21 | """RFC2104 HMAC class. |
| 22 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 23 | This supports the API for Cryptographic Hash Functions (PEP 247). |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 24 | """ |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 25 | |
| 26 | def __init__(self, key, msg = None, digestmod = None): |
| 27 | """Create a new HMAC object. |
| 28 | |
| 29 | key: key for the keyed hash object. |
| 30 | msg: Initial input for the hash, if provided. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 31 | digestmod: A module supporting PEP 247. *OR* |
| 32 | A hashlib constructor returning a new hash object. |
| 33 | Defaults to hashlib.md5. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 34 | """ |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 35 | |
| 36 | if key is _secret_backdoor_key: # cheap |
| 37 | return |
| 38 | |
Raymond Hettinger | 7fdfc2d | 2002-05-31 17:49:10 +0000 | [diff] [blame] | 39 | if digestmod is None: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 40 | import hashlib |
| 41 | digestmod = hashlib.md5 |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 42 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 43 | if callable(digestmod): |
| 44 | self.digest_cons = digestmod |
| 45 | else: |
| 46 | self.digest_cons = lambda d='': digestmod.new(d) |
| 47 | |
| 48 | self.outer = self.digest_cons() |
| 49 | self.inner = self.digest_cons() |
| 50 | self.digest_size = self.inner.digest_size |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 51 | |
Gregory P. Smith | a1e5387 | 2007-11-06 00:32:04 +0000 | [diff] [blame] | 52 | if hasattr(self.inner, 'block_size'): |
| 53 | blocksize = self.inner.block_size |
| 54 | if blocksize < 16: |
| 55 | # Very low blocksize, most likely a legacy value like |
| 56 | # Lib/sha.py and Lib/md5.py have. |
| 57 | blocksize = 64 |
| 58 | else: |
| 59 | blocksize = 64 |
| 60 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 61 | ipad = "\x36" * blocksize |
| 62 | opad = "\x5C" * blocksize |
| 63 | |
| 64 | if len(key) > blocksize: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 65 | key = self.digest_cons(key).digest() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 66 | |
| 67 | key = key + chr(0) * (blocksize - len(key)) |
| 68 | self.outer.update(_strxor(key, opad)) |
| 69 | self.inner.update(_strxor(key, ipad)) |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 70 | if msg is not None: |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 71 | self.update(msg) |
| 72 | |
| 73 | ## def clear(self): |
| 74 | ## raise NotImplementedError, "clear() method not available in HMAC." |
| 75 | |
| 76 | def update(self, msg): |
| 77 | """Update this hashing object with the string msg. |
| 78 | """ |
| 79 | self.inner.update(msg) |
| 80 | |
| 81 | def copy(self): |
| 82 | """Return a separate copy of this hashing object. |
| 83 | |
| 84 | An update to this copy won't affect the original object. |
| 85 | """ |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 86 | other = HMAC(_secret_backdoor_key) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 87 | other.digest_cons = self.digest_cons |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 88 | other.digest_size = self.digest_size |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 89 | other.inner = self.inner.copy() |
| 90 | other.outer = self.outer.copy() |
| 91 | return other |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 92 | |
| 93 | def digest(self): |
| 94 | """Return the hash value of this hashing object. |
| 95 | |
| 96 | This returns a string containing 8-bit data. The object is |
| 97 | not altered in any way by this function; you can continue |
| 98 | updating the object after calling this function. |
| 99 | """ |
| 100 | h = self.outer.copy() |
| 101 | h.update(self.inner.digest()) |
| 102 | return h.digest() |
| 103 | |
| 104 | def hexdigest(self): |
| 105 | """Like digest(), but returns a string of hexadecimal digits instead. |
| 106 | """ |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 107 | return "".join([hex(ord(x))[2:].zfill(2) |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 108 | for x in tuple(self.digest())]) |
| 109 | |
| 110 | def new(key, msg = None, digestmod = None): |
| 111 | """Create a new hashing object and return it. |
| 112 | |
| 113 | key: The starting key for the hash. |
| 114 | msg: if available, will immediately be hashed into the object's starting |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 115 | state. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 116 | |
| 117 | You can now feed arbitrary strings into the object using its update() |
| 118 | method, and can ask for the hash value at any time by calling its digest() |
| 119 | method. |
| 120 | """ |
| 121 | return HMAC(key, msg, digestmod) |