MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / HMAC

Class HMAC

tools/python-3.11.9-amd64/Lib/hmac.py:27–165  ·  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

25
26
27class HMAC:
28 """RFC 2104 HMAC class. Also complies with RFC 4231.
29
30 This supports the API for Cryptographic Hash Functions (PEP 247).
31 """
32 blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
33
34 __slots__ = (
35 "_hmac", "_inner", "_outer", "block_size", "digest_size"
36 )
37
38 def __init__(self, key, msg=None, digestmod=''):
39 """Create a new HMAC object.
40
41 key: bytes or buffer, key for the keyed hash object.
42 msg: bytes or buffer, Initial input for the hash or None.
43 digestmod: A hash name suitable for hashlib.new(). *OR*
44 A hashlib constructor returning a new hash object. *OR*
45 A module supporting PEP 247.
46
47 Required as of 3.8, despite its position after the optional
48 msg argument. Passing it as a keyword argument is
49 recommended, though not required for legacy API reasons.
50 """
51
52 if not isinstance(key, (bytes, bytearray)):
53 raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
54
55 if not digestmod:
56 raise TypeError("Missing required argument 'digestmod'.")
57
58 if _hashopenssl and isinstance(digestmod, (str, _functype)):
59 try:
60 self._init_hmac(key, msg, digestmod)
61 except _hashopenssl.UnsupportedDigestmodError:
62 self._init_old(key, msg, digestmod)
63 else:
64 self._init_old(key, msg, digestmod)
65
66 def _init_hmac(self, key, msg, digestmod):
67 self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
68 self.digest_size = self._hmac.digest_size
69 self.block_size = self._hmac.block_size
70
71 def _init_old(self, key, msg, digestmod):
72 if callable(digestmod):
73 digest_cons = digestmod
74 elif isinstance(digestmod, str):
75 digest_cons = lambda d=b'': _hashlib.new(digestmod, d)
76 else:
77 digest_cons = lambda d=b'': digestmod.new(d)
78
79 self._hmac = None
80 self._outer = digest_cons()
81 self._inner = digest_cons()
82 self.digest_size = self._inner.digest_size
83
84 if hasattr(self._inner, 'block_size'):

Callers 1

newFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected