1 """HMAC (Keyed-Hashing for Message Authentication) Python module.
3 Implements the HMAC algorithm as described by RFC 2104.
6 import warnings
as _warnings
8 trans_5C
= bytes((x ^
0x5C) for x
in range(256))
9 trans_36
= bytes((x ^
0x36) for x
in range(256))
11 # The size of the digests returned by HMAC depends on the underlying
12 # hashing module used. Use digest_size from the instance of HMAC instead.
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
18 _secret_backdoor_key
= []
21 """RFC 2104 HMAC class. Also complies with RFC 4231.
23 This supports the API for Cryptographic Hash Functions (PEP 247).
25 blocksize
= 64 # 512-bit HMAC; can be changed in subclasses.
27 def __init__(self
, key
, msg
= None, digestmod
= None):
28 """Create a new HMAC object.
30 key: key for the keyed hash object.
31 msg: Initial input for the hash, if provided.
32 digestmod: A module supporting PEP 247. *OR*
33 A hashlib constructor returning a new hash object.
34 Defaults to hashlib.md5.
36 Note: key and msg must be bytes objects.
39 if key
is _secret_backdoor_key
: # cheap
42 if not isinstance(key
, bytes
):
43 raise TypeError("expected bytes, but got %r" % type(key
).__name
__)
47 digestmod
= hashlib
.md5
49 if hasattr(digestmod
, '__call__'):
50 self
.digest_cons
= digestmod
52 self
.digest_cons
= lambda d
=b
'': digestmod
.new(d
)
54 self
.outer
= self
.digest_cons()
55 self
.inner
= self
.digest_cons()
56 self
.digest_size
= self
.inner
.digest_size
58 if hasattr(self
.inner
, 'block_size'):
59 blocksize
= self
.inner
.block_size
61 # Very low blocksize, most likely a legacy value like
62 # Lib/sha.py and Lib/md5.py have.
63 _warnings
.warn('block_size of %d seems too small; using our '
64 'default of %d.' % (blocksize
, self
.blocksize
),
66 blocksize
= self
.blocksize
68 _warnings
.warn('No block_size attribute on given digest object; '
69 'Assuming %d.' % (self
.blocksize
),
71 blocksize
= self
.blocksize
73 if len(key
) > blocksize
:
74 key
= self
.digest_cons(key
).digest()
76 key
= key
+ bytes(blocksize
- len(key
))
77 self
.outer
.update(key
.translate(trans_5C
))
78 self
.inner
.update(key
.translate(trans_36
))
83 ## raise NotImplementedError, "clear() method not available in HMAC."
85 def update(self
, msg
):
86 """Update this hashing object with the string msg.
88 if not isinstance(msg
, bytes
):
89 raise TypeError("expected bytes, but got %r" % type(msg
).__name
__)
90 self
.inner
.update(msg
)
93 """Return a separate copy of this hashing object.
95 An update to this copy won't affect the original object.
97 other
= self
.__class
__(_secret_backdoor_key
)
98 other
.digest_cons
= self
.digest_cons
99 other
.digest_size
= self
.digest_size
100 other
.inner
= self
.inner
.copy()
101 other
.outer
= self
.outer
.copy()
105 """Return a hash object for the current state.
107 To be used only internally with digest() and hexdigest().
109 h
= self
.outer
.copy()
110 h
.update(self
.inner
.digest())
114 """Return the hash value of this hashing object.
116 This returns a string containing 8-bit data. The object is
117 not altered in any way by this function; you can continue
118 updating the object after calling this function.
124 """Like digest(), but returns a string of hexadecimal digits instead.
129 def new(key
, msg
= None, digestmod
= None):
130 """Create a new hashing object and return it.
132 key: The starting key for the hash.
133 msg: if available, will immediately be hashed into the object's starting
136 You can now feed arbitrary strings into the object using its update()
137 method, and can ask for the hash value at any time by calling its digest()
140 return HMAC(key
, msg
, digestmod
)