MCPcopy Index your code
hub / github.com/python/cpython / HMAC

Class HMAC

Lib/hmac.py:52–210  ·  view source on GitHub ↗

RFC 2104 HMAC class. Also complies with RFC 4231. This supports the API for Cryptographic Hash Functions (PEP 247).

Source from the content-addressed store, hash-verified

50
51
52class HMAC:
53 """RFC 2104 HMAC class. Also complies with RFC 4231.
54
55 This supports the API for Cryptographic Hash Functions (PEP 247).
56 """
57
58 # Note: self.blocksize is the default blocksize; self.block_size
59 # is effective block size as well as the public API attribute.
60 blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
61
62 __slots__ = (
63 "_hmac", "_inner", "_outer", "block_size", "digest_size"
64 )
65
66 def __init__(self, key, msg=None, digestmod=''):
67 """Create a new HMAC object.
68
69 key: bytes or buffer, key for the keyed hash object.
70 msg: bytes or buffer, Initial input for the hash or None.
71 digestmod: A hash name suitable for hashlib.new(). *OR*
72 A hashlib constructor returning a new hash object. *OR*
73 A module supporting PEP 247.
74
75 Required as of 3.8, despite its position after the optional
76 msg argument. Passing it as a keyword argument is
77 recommended, though not required for legacy API reasons.
78 """
79
80 if not isinstance(key, (bytes, bytearray)):
81 raise TypeError(f"key: expected bytes or bytearray, "
82 f"but got {type(key).__name__!r}")
83
84 if not digestmod:
85 raise TypeError("Missing required argument 'digestmod'.")
86
87 self.__init(key, msg, digestmod)
88
89 def __init(self, key, msg, digestmod):
90 if _hashopenssl and isinstance(digestmod, (str, _functype)):
91 try:
92 self._init_openssl_hmac(key, msg, digestmod)
93 return
94 except _hashopenssl.UnsupportedDigestmodError: # pragma: no cover
95 pass
96 if _hmac and isinstance(digestmod, str):
97 try:
98 self._init_builtin_hmac(key, msg, digestmod)
99 return
100 except _hmac.UnknownHashError: # pragma: no cover
101 pass
102 self._init_old(key, msg, digestmod)
103
104 def _init_openssl_hmac(self, key, msg, digestmod):
105 self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
106 self._inner = self._outer = None # because the slots are defined
107 self.digest_size = self._hmac.digest_size
108 self.block_size = self._hmac.block_size
109

Callers 1

newFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…