blob: 41d6c6cbd71225cd9bb73bed73ec4cb9102eb488 [file] [log] [blame]
Guido van Rossum8ceef412001-09-11 15:54:00 +00001"""HMAC (Keyed-Hashing for Message Authentication) Python module.
2
3Implements the HMAC algorithm as described by RFC 2104.
4"""
5
Guido van Rossum8ceef412001-09-11 15:54:00 +00006def _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 Petersb64bec32001-09-18 02:26:39 +000010
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000011# The size of the digests returned by HMAC depends on the underlying
12# hashing module used.
13digest_size = None
14
Tim Peters934d31b2004-03-20 20:11:29 +000015# 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 Rossum8ceef412001-09-11 15:54:00 +000020class HMAC:
21 """RFC2104 HMAC class.
22
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000023 This supports the API for Cryptographic Hash Functions (PEP 247).
Tim Petersb64bec32001-09-18 02:26:39 +000024 """
Guido van Rossum8ceef412001-09-11 15:54:00 +000025
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. Smithf21a5f72005-08-21 18:45:59 +000031 digestmod: A module supporting PEP 247. *OR*
32 A hashlib constructor returning a new hash object.
33 Defaults to hashlib.md5.
Guido van Rossum8ceef412001-09-11 15:54:00 +000034 """
Tim Peters934d31b2004-03-20 20:11:29 +000035
36 if key is _secret_backdoor_key: # cheap
37 return
38
Raymond Hettinger7fdfc2d2002-05-31 17:49:10 +000039 if digestmod is None:
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000040 import hashlib
41 digestmod = hashlib.md5
Guido van Rossum8ceef412001-09-11 15:54:00 +000042
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000043 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 Peters88768482001-11-13 21:51:26 +000051
Guido van Rossum8ceef412001-09-11 15:54:00 +000052 blocksize = 64
53 ipad = "\x36" * blocksize
54 opad = "\x5C" * blocksize
55
56 if len(key) > blocksize:
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000057 key = self.digest_cons(key).digest()
Guido van Rossum8ceef412001-09-11 15:54:00 +000058
59 key = key + chr(0) * (blocksize - len(key))
60 self.outer.update(_strxor(key, opad))
61 self.inner.update(_strxor(key, ipad))
Raymond Hettinger094662a2002-06-01 01:29:16 +000062 if msg is not None:
Guido van Rossum8ceef412001-09-11 15:54:00 +000063 self.update(msg)
64
65## def clear(self):
66## raise NotImplementedError, "clear() method not available in HMAC."
67
68 def update(self, msg):
69 """Update this hashing object with the string msg.
70 """
71 self.inner.update(msg)
72
73 def copy(self):
74 """Return a separate copy of this hashing object.
75
76 An update to this copy won't affect the original object.
77 """
Tim Peters934d31b2004-03-20 20:11:29 +000078 other = HMAC(_secret_backdoor_key)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000079 other.digest_cons = self.digest_cons
Tim Peters934d31b2004-03-20 20:11:29 +000080 other.digest_size = self.digest_size
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000081 other.inner = self.inner.copy()
82 other.outer = self.outer.copy()
83 return other
Guido van Rossum8ceef412001-09-11 15:54:00 +000084
85 def digest(self):
86 """Return the hash value of this hashing object.
87
88 This returns a string containing 8-bit data. The object is
89 not altered in any way by this function; you can continue
90 updating the object after calling this function.
91 """
92 h = self.outer.copy()
93 h.update(self.inner.digest())
94 return h.digest()
95
96 def hexdigest(self):
97 """Like digest(), but returns a string of hexadecimal digits instead.
98 """
Walter Dörwald65230a22002-06-03 15:58:32 +000099 return "".join([hex(ord(x))[2:].zfill(2)
Guido van Rossum8ceef412001-09-11 15:54:00 +0000100 for x in tuple(self.digest())])
101
102def new(key, msg = None, digestmod = None):
103 """Create a new hashing object and return it.
104
105 key: The starting key for the hash.
106 msg: if available, will immediately be hashed into the object's starting
Tim Petersb64bec32001-09-18 02:26:39 +0000107 state.
Guido van Rossum8ceef412001-09-11 15:54:00 +0000108
109 You can now feed arbitrary strings into the object using its update()
110 method, and can ask for the hash value at any time by calling its digest()
111 method.
112 """
113 return HMAC(key, msg, digestmod)