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 | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 6 | import warnings as _warnings |
| 7 | |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 8 | trans_5C = bytes((x ^ 0x5C) for x in range(256)) |
| 9 | trans_36 = bytes((x ^ 0x36) for x in range(256)) |
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 |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 12 | # hashing module used. Use digest_size from the instance of HMAC instead. |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 13 | digest_size = None |
| 14 | |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 16 | class HMAC: |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 17 | """RFC 2104 HMAC class. Also complies with RFC 4231. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 19 | This supports the API for Cryptographic Hash Functions (PEP 247). |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 20 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 21 | blocksize = 64 # 512-bit HMAC; can be changed in subclasses. |
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. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 28 | digestmod: A module supporting PEP 247. *OR* |
| 29 | A hashlib constructor returning a new hash object. |
| 30 | Defaults to hashlib.md5. |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 31 | |
| 32 | Note: key and msg must be bytes objects. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 33 | """ |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 34 | |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 35 | if not isinstance(key, bytes): |
| 36 | raise TypeError("expected bytes, but got %r" % type(key).__name__) |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 37 | |
Raymond Hettinger | 7fdfc2d | 2002-05-31 17:49:10 +0000 | [diff] [blame] | 38 | if digestmod is None: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 39 | import hashlib |
| 40 | digestmod = hashlib.md5 |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 42 | if hasattr(digestmod, '__call__'): |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 43 | self.digest_cons = digestmod |
| 44 | else: |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 45 | self.digest_cons = lambda d=b'': digestmod.new(d) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 46 | |
| 47 | self.outer = self.digest_cons() |
| 48 | self.inner = self.digest_cons() |
| 49 | self.digest_size = self.inner.digest_size |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 51 | if hasattr(self.inner, 'block_size'): |
| 52 | blocksize = self.inner.block_size |
| 53 | if blocksize < 16: |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 54 | _warnings.warn('block_size of %d seems too small; using our ' |
| 55 | 'default of %d.' % (blocksize, self.blocksize), |
| 56 | RuntimeWarning, 2) |
| 57 | blocksize = self.blocksize |
| 58 | else: |
| 59 | _warnings.warn('No block_size attribute on given digest object; ' |
| 60 | 'Assuming %d.' % (self.blocksize), |
| 61 | RuntimeWarning, 2) |
| 62 | blocksize = self.blocksize |
| 63 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 67 | key = key + bytes(blocksize - len(key)) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 68 | self.outer.update(key.translate(trans_5C)) |
| 69 | self.inner.update(key.translate(trans_36)) |
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 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 73 | def update(self, msg): |
| 74 | """Update this hashing object with the string msg. |
| 75 | """ |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 76 | if not isinstance(msg, bytes): |
| 77 | raise TypeError("expected bytes, but got %r" % type(msg).__name__) |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 78 | self.inner.update(msg) |
| 79 | |
| 80 | def copy(self): |
| 81 | """Return a separate copy of this hashing object. |
| 82 | |
| 83 | An update to this copy won't affect the original object. |
| 84 | """ |
Benjamin Peterson | 0cc7444 | 2010-08-21 02:45:15 +0000 | [diff] [blame] | 85 | # Call __new__ directly to avoid the expensive __init__. |
| 86 | other = self.__class__.__new__(self.__class__) |
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 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 93 | def _current(self): |
| 94 | """Return a hash object for the current state. |
| 95 | |
| 96 | To be used only internally with digest() and hexdigest(). |
| 97 | """ |
| 98 | h = self.outer.copy() |
| 99 | h.update(self.inner.digest()) |
| 100 | return h |
| 101 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 102 | def digest(self): |
| 103 | """Return the hash value of this hashing object. |
| 104 | |
| 105 | This returns a string containing 8-bit data. The object is |
| 106 | not altered in any way by this function; you can continue |
| 107 | updating the object after calling this function. |
| 108 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 109 | h = self._current() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 110 | return h.digest() |
| 111 | |
| 112 | def hexdigest(self): |
| 113 | """Like digest(), but returns a string of hexadecimal digits instead. |
| 114 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 115 | h = self._current() |
| 116 | return h.hexdigest() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 117 | |
| 118 | def new(key, msg = None, digestmod = None): |
| 119 | """Create a new hashing object and return it. |
| 120 | |
| 121 | key: The starting key for the hash. |
| 122 | msg: if available, will immediately be hashed into the object's starting |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 123 | state. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 124 | |
| 125 | You can now feed arbitrary strings into the object using its update() |
| 126 | method, and can ask for the hash value at any time by calling its digest() |
| 127 | method. |
| 128 | """ |
| 129 | return HMAC(key, msg, digestmod) |