blob: df2bffd22911e161096a33756a221f722c3e1fab [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
Andrew M. Kuchling8fe2d202006-12-19 14:13:05 +00006trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
7trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
Tim Petersb64bec32001-09-18 02:26:39 +00008
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +00009# The size of the digests returned by HMAC depends on the underlying
10# hashing module used.
11digest_size = None
12
Tim Peters934d31b2004-03-20 20:11:29 +000013# A unique object passed by HMAC.copy() to the HMAC constructor, in order
14# that the latter return very quickly. HMAC("") in contrast is quite
15# expensive.
16_secret_backdoor_key = []
17
Guido van Rossum8ceef412001-09-11 15:54:00 +000018class HMAC:
19 """RFC2104 HMAC class.
20
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000021 This supports the API for Cryptographic Hash Functions (PEP 247).
Tim Petersb64bec32001-09-18 02:26:39 +000022 """
Guido van Rossum8ceef412001-09-11 15:54:00 +000023
24 def __init__(self, key, msg = None, digestmod = None):
25 """Create a new HMAC object.
26
27 key: key for the keyed hash object.
28 msg: Initial input for the hash, if provided.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000029 digestmod: A module supporting PEP 247. *OR*
30 A hashlib constructor returning a new hash object.
31 Defaults to hashlib.md5.
Guido van Rossum8ceef412001-09-11 15:54:00 +000032 """
Tim Peters934d31b2004-03-20 20:11:29 +000033
34 if key is _secret_backdoor_key: # cheap
35 return
36
Raymond Hettinger7fdfc2d2002-05-31 17:49:10 +000037 if digestmod is None:
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000038 import hashlib
39 digestmod = hashlib.md5
Guido van Rossum8ceef412001-09-11 15:54:00 +000040
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000041 if callable(digestmod):
42 self.digest_cons = digestmod
43 else:
44 self.digest_cons = lambda d='': digestmod.new(d)
45
46 self.outer = self.digest_cons()
47 self.inner = self.digest_cons()
48 self.digest_size = self.inner.digest_size
Tim Peters88768482001-11-13 21:51:26 +000049
Guido van Rossum8ceef412001-09-11 15:54:00 +000050 blocksize = 64
Guido van Rossum8ceef412001-09-11 15:54:00 +000051 if len(key) > blocksize:
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000052 key = self.digest_cons(key).digest()
Guido van Rossum8ceef412001-09-11 15:54:00 +000053
54 key = key + chr(0) * (blocksize - len(key))
Andrew M. Kuchling8fe2d202006-12-19 14:13:05 +000055 self.outer.update(key.translate(trans_5C))
56 self.inner.update(key.translate(trans_36))
Raymond Hettinger094662a2002-06-01 01:29:16 +000057 if msg is not None:
Guido van Rossum8ceef412001-09-11 15:54:00 +000058 self.update(msg)
59
60## def clear(self):
61## raise NotImplementedError, "clear() method not available in HMAC."
62
63 def update(self, msg):
64 """Update this hashing object with the string msg.
65 """
66 self.inner.update(msg)
67
68 def copy(self):
69 """Return a separate copy of this hashing object.
70
71 An update to this copy won't affect the original object.
72 """
Tim Peters934d31b2004-03-20 20:11:29 +000073 other = HMAC(_secret_backdoor_key)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000074 other.digest_cons = self.digest_cons
Tim Peters934d31b2004-03-20 20:11:29 +000075 other.digest_size = self.digest_size
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000076 other.inner = self.inner.copy()
77 other.outer = self.outer.copy()
78 return other
Guido van Rossum8ceef412001-09-11 15:54:00 +000079
80 def digest(self):
81 """Return the hash value of this hashing object.
82
83 This returns a string containing 8-bit data. The object is
84 not altered in any way by this function; you can continue
85 updating the object after calling this function.
86 """
87 h = self.outer.copy()
88 h.update(self.inner.digest())
89 return h.digest()
90
91 def hexdigest(self):
92 """Like digest(), but returns a string of hexadecimal digits instead.
93 """
Walter Dörwald65230a22002-06-03 15:58:32 +000094 return "".join([hex(ord(x))[2:].zfill(2)
Guido van Rossum8ceef412001-09-11 15:54:00 +000095 for x in tuple(self.digest())])
96
97def new(key, msg = None, digestmod = None):
98 """Create a new hashing object and return it.
99
100 key: The starting key for the hash.
101 msg: if available, will immediately be hashed into the object's starting
Tim Petersb64bec32001-09-18 02:26:39 +0000102 state.
Guido van Rossum8ceef412001-09-11 15:54:00 +0000103
104 You can now feed arbitrary strings into the object using its update()
105 method, and can ask for the hash value at any time by calling its digest()
106 method.
107 """
108 return HMAC(key, msg, digestmod)