diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-04-04 14:31:37 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-04-04 14:41:27 +0300 |
commit | 8ca7961c8ff183dca63ff964f6142d24df5e03e8 (patch) | |
tree | a40fefbc0747d2a2e103dabdc392f6f13b628ab6 /contrib/python/PyJWT/py2/jwt/compat.py | |
parent | def02a26e8270f5177fd53cfcf8ce445e26bf583 (diff) | |
download | ydb-8ca7961c8ff183dca63ff964f6142d24df5e03e8.tar.gz |
Intermediate changes
Diffstat (limited to 'contrib/python/PyJWT/py2/jwt/compat.py')
-rw-r--r-- | contrib/python/PyJWT/py2/jwt/compat.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/contrib/python/PyJWT/py2/jwt/compat.py b/contrib/python/PyJWT/py2/jwt/compat.py new file mode 100644 index 00000000000..e79e258e56d --- /dev/null +++ b/contrib/python/PyJWT/py2/jwt/compat.py @@ -0,0 +1,68 @@ +""" +The `compat` module provides support for backwards compatibility with older +versions of python, and compatibility wrappers around optional packages. +""" +# flake8: noqa +import hmac +import struct +import sys + + +PY3 = sys.version_info[0] == 3 + + +if PY3: + text_type = str + binary_type = bytes +else: + text_type = unicode + binary_type = str + +string_types = (text_type, binary_type) + +try: + # Importing ABCs from collections will be removed in PY3.8 + from collections.abc import Iterable, Mapping +except ImportError: + from collections import Iterable, Mapping + +try: + constant_time_compare = hmac.compare_digest +except AttributeError: + # Fallback for Python < 2.7 + def constant_time_compare(val1, val2): + """ + Returns True if the two strings are equal, False otherwise. + + The time taken is independent of the number of characters that match. + """ + if len(val1) != len(val2): + return False + + result = 0 + + for x, y in zip(val1, val2): + result |= ord(x) ^ ord(y) + + return result == 0 + +# Use int.to_bytes if it exists (Python 3) +if getattr(int, 'to_bytes', None): + def bytes_from_int(val): + remaining = val + byte_length = 0 + + while remaining != 0: + remaining = remaining >> 8 + byte_length += 1 + + return val.to_bytes(byte_length, 'big', signed=False) +else: + def bytes_from_int(val): + buf = [] + while val: + val, remainder = divmod(val, 256) + buf.append(remainder) + + buf.reverse() + return struct.pack('%sB' % len(buf), *buf) |