[Part of patch #1182394] Move the HMAC blocksize to be a class-level
[python.git] / Lib / hmac.py
blobed5afc711ae54fbcd313fbffdbd643d0e3d2deca
1 """HMAC (Keyed-Hashing for Message Authentication) Python module.
3 Implements the HMAC algorithm as described by RFC 2104.
4 """
6 trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
7 trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
9 # The size of the digests returned by HMAC depends on the underlying
10 # hashing module used. Use digest_size from the instance of HMAC instead.
11 digest_size = None
13 # 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 = []
18 class HMAC:
19 """RFC2104 HMAC class.
21 This supports the API for Cryptographic Hash Functions (PEP 247).
22 """
23 blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
25 def __init__(self, key, msg = None, digestmod = None):
26 """Create a new HMAC object.
28 key: key for the keyed hash object.
29 msg: Initial input for the hash, if provided.
30 digestmod: A module supporting PEP 247. *OR*
31 A hashlib constructor returning a new hash object.
32 Defaults to hashlib.md5.
33 """
35 if key is _secret_backdoor_key: # cheap
36 return
38 if digestmod is None:
39 import hashlib
40 digestmod = hashlib.md5
42 if callable(digestmod):
43 self.digest_cons = digestmod
44 else:
45 self.digest_cons = lambda d='': digestmod.new(d)
47 self.outer = self.digest_cons()
48 self.inner = self.digest_cons()
49 self.digest_size = self.inner.digest_size
51 blocksize = self.blocksize
52 if len(key) > blocksize:
53 key = self.digest_cons(key).digest()
55 key = key + chr(0) * (blocksize - len(key))
56 self.outer.update(key.translate(trans_5C))
57 self.inner.update(key.translate(trans_36))
58 if msg is not None:
59 self.update(msg)
61 ## def clear(self):
62 ## raise NotImplementedError, "clear() method not available in HMAC."
64 def update(self, msg):
65 """Update this hashing object with the string msg.
66 """
67 self.inner.update(msg)
69 def copy(self):
70 """Return a separate copy of this hashing object.
72 An update to this copy won't affect the original object.
73 """
74 other = self.__class__(_secret_backdoor_key)
75 other.digest_cons = self.digest_cons
76 other.digest_size = self.digest_size
77 other.inner = self.inner.copy()
78 other.outer = self.outer.copy()
79 return other
81 def digest(self):
82 """Return the hash value of this hashing object.
84 This returns a string containing 8-bit data. The object is
85 not altered in any way by this function; you can continue
86 updating the object after calling this function.
87 """
88 h = self.outer.copy()
89 h.update(self.inner.digest())
90 return h.digest()
92 def hexdigest(self):
93 """Like digest(), but returns a string of hexadecimal digits instead.
94 """
95 return "".join([hex(ord(x))[2:].zfill(2)
96 for x in tuple(self.digest())])
98 def new(key, msg = None, digestmod = None):
99 """Create a new hashing object and return it.
101 key: The starting key for the hash.
102 msg: if available, will immediately be hashed into the object's starting
103 state.
105 You can now feed arbitrary strings into the object using its update()
106 method, and can ask for the hash value at any time by calling its digest()
107 method.
109 return HMAC(key, msg, digestmod)