diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Lib/hmac.py | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Lib/hmac.py')
-rw-r--r-- | contrib/tools/python3/src/Lib/hmac.py | 132 |
1 files changed, 66 insertions, 66 deletions
diff --git a/contrib/tools/python3/src/Lib/hmac.py b/contrib/tools/python3/src/Lib/hmac.py index 180bc378b5..3051334b3d 100644 --- a/contrib/tools/python3/src/Lib/hmac.py +++ b/contrib/tools/python3/src/Lib/hmac.py @@ -1,4 +1,4 @@ -"""HMAC (Keyed-Hashing for Message Authentication) module. +"""HMAC (Keyed-Hashing for Message Authentication) module. Implements the HMAC algorithm as described by RFC 2104. """ @@ -9,10 +9,10 @@ try: except ImportError: _hashopenssl = None _openssl_md_meths = None - from _operator import _compare_digest as compare_digest + from _operator import _compare_digest as compare_digest else: _openssl_md_meths = frozenset(_hashopenssl.openssl_md_meth_names) - compare_digest = _hashopenssl.compare_digest + compare_digest = _hashopenssl.compare_digest import hashlib as _hashlib trans_5C = bytes((x ^ 0x5C) for x in range(256)) @@ -31,43 +31,43 @@ class HMAC: """ blocksize = 64 # 512-bit HMAC; can be changed in subclasses. - __slots__ = ( - "_digest_cons", "_inner", "_outer", "block_size", "digest_size" - ) - - def __init__(self, key, msg=None, digestmod=''): + __slots__ = ( + "_digest_cons", "_inner", "_outer", "block_size", "digest_size" + ) + + def __init__(self, key, msg=None, digestmod=''): """Create a new HMAC object. - key: bytes or buffer, key for the keyed hash object. - msg: bytes or buffer, Initial input for the hash or None. - digestmod: A hash name suitable for hashlib.new(). *OR* + key: bytes or buffer, key for the keyed hash object. + msg: bytes or buffer, Initial input for the hash or None. + digestmod: A hash name suitable for hashlib.new(). *OR* A hashlib constructor returning a new hash object. *OR* - A module supporting PEP 247. + A module supporting PEP 247. - Required as of 3.8, despite its position after the optional - msg argument. Passing it as a keyword argument is - recommended, though not required for legacy API reasons. + Required as of 3.8, despite its position after the optional + msg argument. Passing it as a keyword argument is + recommended, though not required for legacy API reasons. """ if not isinstance(key, (bytes, bytearray)): raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) - if not digestmod: - raise TypeError("Missing required parameter 'digestmod'.") + if not digestmod: + raise TypeError("Missing required parameter 'digestmod'.") if callable(digestmod): - self._digest_cons = digestmod + self._digest_cons = digestmod elif isinstance(digestmod, str): - self._digest_cons = lambda d=b'': _hashlib.new(digestmod, d) + self._digest_cons = lambda d=b'': _hashlib.new(digestmod, d) else: - self._digest_cons = lambda d=b'': digestmod.new(d) + self._digest_cons = lambda d=b'': digestmod.new(d) - self._outer = self._digest_cons() - self._inner = self._digest_cons() - self.digest_size = self._inner.digest_size + self._outer = self._digest_cons() + self._inner = self._digest_cons() + self.digest_size = self._inner.digest_size - if hasattr(self._inner, 'block_size'): - blocksize = self._inner.block_size + if hasattr(self._inner, 'block_size'): + blocksize = self._inner.block_size if blocksize < 16: _warnings.warn('block_size of %d seems too small; using our ' 'default of %d.' % (blocksize, self.blocksize), @@ -84,33 +84,33 @@ class HMAC: self.block_size = blocksize if len(key) > blocksize: - key = self._digest_cons(key).digest() + key = self._digest_cons(key).digest() key = key.ljust(blocksize, b'\0') - self._outer.update(key.translate(trans_5C)) - self._inner.update(key.translate(trans_36)) + self._outer.update(key.translate(trans_5C)) + self._inner.update(key.translate(trans_36)) if msg is not None: self.update(msg) @property def name(self): - return "hmac-" + self._inner.name - - @property - def digest_cons(self): - return self._digest_cons - - @property - def inner(self): - return self._inner - - @property - def outer(self): - return self._outer - + return "hmac-" + self._inner.name + + @property + def digest_cons(self): + return self._digest_cons + + @property + def inner(self): + return self._inner + + @property + def outer(self): + return self._outer + def update(self, msg): - """Feed data from msg into this hashing object.""" - self._inner.update(msg) + """Feed data from msg into this hashing object.""" + self._inner.update(msg) def copy(self): """Return a separate copy of this hashing object. @@ -119,10 +119,10 @@ class HMAC: """ # Call __new__ directly to avoid the expensive __init__. other = self.__class__.__new__(self.__class__) - other._digest_cons = self._digest_cons + other._digest_cons = self._digest_cons other.digest_size = self.digest_size - other._inner = self._inner.copy() - other._outer = self._outer.copy() + other._inner = self._inner.copy() + other._outer = self._outer.copy() return other def _current(self): @@ -130,14 +130,14 @@ class HMAC: To be used only internally with digest() and hexdigest(). """ - h = self._outer.copy() - h.update(self._inner.digest()) + h = self._outer.copy() + h.update(self._inner.digest()) return h def digest(self): """Return the hash value of this hashing object. - This returns the hmac value as bytes. The object is + This returns the hmac value as bytes. The object is not altered in any way by this function; you can continue updating the object after calling this function. """ @@ -150,31 +150,31 @@ class HMAC: h = self._current() return h.hexdigest() -def new(key, msg=None, digestmod=''): +def new(key, msg=None, digestmod=''): """Create a new hashing object and return it. - key: bytes or buffer, The starting key for the hash. - msg: bytes or buffer, Initial input for the hash, or None. - digestmod: A hash name suitable for hashlib.new(). *OR* - A hashlib constructor returning a new hash object. *OR* - A module supporting PEP 247. - - Required as of 3.8, despite its position after the optional - msg argument. Passing it as a keyword argument is - recommended, though not required for legacy API reasons. - - You can now feed arbitrary bytes into the object using its update() + key: bytes or buffer, The starting key for the hash. + msg: bytes or buffer, Initial input for the hash, or None. + digestmod: A hash name suitable for hashlib.new(). *OR* + A hashlib constructor returning a new hash object. *OR* + A module supporting PEP 247. + + Required as of 3.8, despite its position after the optional + msg argument. Passing it as a keyword argument is + recommended, though not required for legacy API reasons. + + You can now feed arbitrary bytes into the object using its update() method, and can ask for the hash value at any time by calling its digest() - or hexdigest() methods. + or hexdigest() methods. """ return HMAC(key, msg, digestmod) def digest(key, msg, digest): - """Fast inline implementation of HMAC. + """Fast inline implementation of HMAC. - key: bytes or buffer, The key for the keyed hash object. - msg: bytes or buffer, Input message. + key: bytes or buffer, The key for the keyed hash object. + msg: bytes or buffer, Input message. digest: A hash name suitable for hashlib.new() for best performance. *OR* A hashlib constructor returning a new hash object. *OR* A module supporting PEP 247. |