1 | n/a | """HMAC (Keyed-Hashing for Message Authentication) Python module. |
---|
2 | n/a | |
---|
3 | n/a | Implements the HMAC algorithm as described by RFC 2104. |
---|
4 | n/a | """ |
---|
5 | n/a | |
---|
6 | n/a | import warnings as _warnings |
---|
7 | n/a | from _operator import _compare_digest as compare_digest |
---|
8 | n/a | import hashlib as _hashlib |
---|
9 | n/a | |
---|
10 | n/a | trans_5C = bytes((x ^ 0x5C) for x in range(256)) |
---|
11 | n/a | trans_36 = bytes((x ^ 0x36) for x in range(256)) |
---|
12 | n/a | |
---|
13 | n/a | # The size of the digests returned by HMAC depends on the underlying |
---|
14 | n/a | # hashing module used. Use digest_size from the instance of HMAC instead. |
---|
15 | n/a | digest_size = None |
---|
16 | n/a | |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | class HMAC: |
---|
20 | n/a | """RFC 2104 HMAC class. Also complies with RFC 4231. |
---|
21 | n/a | |
---|
22 | n/a | This supports the API for Cryptographic Hash Functions (PEP 247). |
---|
23 | n/a | """ |
---|
24 | n/a | blocksize = 64 # 512-bit HMAC; can be changed in subclasses. |
---|
25 | n/a | |
---|
26 | n/a | def __init__(self, key, msg = None, digestmod = None): |
---|
27 | n/a | """Create a new HMAC object. |
---|
28 | n/a | |
---|
29 | n/a | key: key for the keyed hash object. |
---|
30 | n/a | msg: Initial input for the hash, if provided. |
---|
31 | n/a | digestmod: A module supporting PEP 247. *OR* |
---|
32 | n/a | A hashlib constructor returning a new hash object. *OR* |
---|
33 | n/a | A hash name suitable for hashlib.new(). |
---|
34 | n/a | Defaults to hashlib.md5. |
---|
35 | n/a | Implicit default to hashlib.md5 is deprecated and will be |
---|
36 | n/a | removed in Python 3.6. |
---|
37 | n/a | |
---|
38 | n/a | Note: key and msg must be a bytes or bytearray objects. |
---|
39 | n/a | """ |
---|
40 | n/a | |
---|
41 | n/a | if not isinstance(key, (bytes, bytearray)): |
---|
42 | n/a | raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) |
---|
43 | n/a | |
---|
44 | n/a | if digestmod is None: |
---|
45 | n/a | _warnings.warn("HMAC() without an explicit digestmod argument " |
---|
46 | n/a | "is deprecated.", PendingDeprecationWarning, 2) |
---|
47 | n/a | digestmod = _hashlib.md5 |
---|
48 | n/a | |
---|
49 | n/a | if callable(digestmod): |
---|
50 | n/a | self.digest_cons = digestmod |
---|
51 | n/a | elif isinstance(digestmod, str): |
---|
52 | n/a | self.digest_cons = lambda d=b'': _hashlib.new(digestmod, d) |
---|
53 | n/a | else: |
---|
54 | n/a | self.digest_cons = lambda d=b'': digestmod.new(d) |
---|
55 | n/a | |
---|
56 | n/a | self.outer = self.digest_cons() |
---|
57 | n/a | self.inner = self.digest_cons() |
---|
58 | n/a | self.digest_size = self.inner.digest_size |
---|
59 | n/a | |
---|
60 | n/a | if hasattr(self.inner, 'block_size'): |
---|
61 | n/a | blocksize = self.inner.block_size |
---|
62 | n/a | if blocksize < 16: |
---|
63 | n/a | _warnings.warn('block_size of %d seems too small; using our ' |
---|
64 | n/a | 'default of %d.' % (blocksize, self.blocksize), |
---|
65 | n/a | RuntimeWarning, 2) |
---|
66 | n/a | blocksize = self.blocksize |
---|
67 | n/a | else: |
---|
68 | n/a | _warnings.warn('No block_size attribute on given digest object; ' |
---|
69 | n/a | 'Assuming %d.' % (self.blocksize), |
---|
70 | n/a | RuntimeWarning, 2) |
---|
71 | n/a | blocksize = self.blocksize |
---|
72 | n/a | |
---|
73 | n/a | # self.blocksize is the default blocksize. self.block_size is |
---|
74 | n/a | # effective block size as well as the public API attribute. |
---|
75 | n/a | self.block_size = blocksize |
---|
76 | n/a | |
---|
77 | n/a | if len(key) > blocksize: |
---|
78 | n/a | key = self.digest_cons(key).digest() |
---|
79 | n/a | |
---|
80 | n/a | key = key.ljust(blocksize, b'\0') |
---|
81 | n/a | self.outer.update(key.translate(trans_5C)) |
---|
82 | n/a | self.inner.update(key.translate(trans_36)) |
---|
83 | n/a | if msg is not None: |
---|
84 | n/a | self.update(msg) |
---|
85 | n/a | |
---|
86 | n/a | @property |
---|
87 | n/a | def name(self): |
---|
88 | n/a | return "hmac-" + self.inner.name |
---|
89 | n/a | |
---|
90 | n/a | def update(self, msg): |
---|
91 | n/a | """Update this hashing object with the string msg. |
---|
92 | n/a | """ |
---|
93 | n/a | self.inner.update(msg) |
---|
94 | n/a | |
---|
95 | n/a | def copy(self): |
---|
96 | n/a | """Return a separate copy of this hashing object. |
---|
97 | n/a | |
---|
98 | n/a | An update to this copy won't affect the original object. |
---|
99 | n/a | """ |
---|
100 | n/a | # Call __new__ directly to avoid the expensive __init__. |
---|
101 | n/a | other = self.__class__.__new__(self.__class__) |
---|
102 | n/a | other.digest_cons = self.digest_cons |
---|
103 | n/a | other.digest_size = self.digest_size |
---|
104 | n/a | other.inner = self.inner.copy() |
---|
105 | n/a | other.outer = self.outer.copy() |
---|
106 | n/a | return other |
---|
107 | n/a | |
---|
108 | n/a | def _current(self): |
---|
109 | n/a | """Return a hash object for the current state. |
---|
110 | n/a | |
---|
111 | n/a | To be used only internally with digest() and hexdigest(). |
---|
112 | n/a | """ |
---|
113 | n/a | h = self.outer.copy() |
---|
114 | n/a | h.update(self.inner.digest()) |
---|
115 | n/a | return h |
---|
116 | n/a | |
---|
117 | n/a | def digest(self): |
---|
118 | n/a | """Return the hash value of this hashing object. |
---|
119 | n/a | |
---|
120 | n/a | This returns a string containing 8-bit data. The object is |
---|
121 | n/a | not altered in any way by this function; you can continue |
---|
122 | n/a | updating the object after calling this function. |
---|
123 | n/a | """ |
---|
124 | n/a | h = self._current() |
---|
125 | n/a | return h.digest() |
---|
126 | n/a | |
---|
127 | n/a | def hexdigest(self): |
---|
128 | n/a | """Like digest(), but returns a string of hexadecimal digits instead. |
---|
129 | n/a | """ |
---|
130 | n/a | h = self._current() |
---|
131 | n/a | return h.hexdigest() |
---|
132 | n/a | |
---|
133 | n/a | def new(key, msg = None, digestmod = None): |
---|
134 | n/a | """Create a new hashing object and return it. |
---|
135 | n/a | |
---|
136 | n/a | key: The starting key for the hash. |
---|
137 | n/a | msg: if available, will immediately be hashed into the object's starting |
---|
138 | n/a | state. |
---|
139 | n/a | |
---|
140 | n/a | You can now feed arbitrary strings into the object using its update() |
---|
141 | n/a | method, and can ask for the hash value at any time by calling its digest() |
---|
142 | n/a | method. |
---|
143 | n/a | """ |
---|
144 | n/a | return HMAC(key, msg, digestmod) |
---|