# Path: lib/util/str.py
# def bytes_to_str(data):
#     '''
#     Converts `data` to `str`.
# 
#     If data is a `bytes`, it is decoded into `str`.
#     If data is `str`, it is returned as-is.
#     '''
#     assert isinstance(data, (str, bytes)), \
#         'data must be str or bytes: %r' % (data)
#     if isinstance(data, str):
#         # already str, yay
#         return data
#     return data.decode()
# 
# def str_to_bytes(data):
#     '''
#     Converts `data` to `bytes`.
# 
#     If data is a `str`, it is encoded into `bytes`.
#     If data is `bytes`, it is returned as-is.
#     '''
#     assert isinstance(data, (str, bytes)), \
#         'data must be str or bytes: %r' % (data)
#     if isinstance(data, bytes):
#         # already bytes, yay
#         return data
#     return data.encode()
# 
# Path: lib/util/format.py
# def base64_encode(data):
#     '''
#     Encodes the given `data` in base-64. The result will either be a `str`,
#     or `bytes`, depending on the input type (same as input type).
#     '''
#     assert isinstance(data, (str, bytes)), \
#         'data must be str or bytes: %r' % (data)
# 
#     is_str = isinstance(data, str)
#     if is_str:
#         data = str_to_bytes(data)
# 
#     encoded = base64.b64encode(data)
# 
#     if is_str:
#         encoded = bytes_to_str(encoded)
# 
#     return encoded


import hashlib
import hmac
import logging
import os
from ..util.str import (
    bytes_to_str,
    str_to_bytes,
)
from ..util.format import (
    base64_encode,
)
#!/usr/bin/env python3

'''
lib/util/hmac.py
Contains HMAC utility functions. The ycmd server expects an HMAC header with
all requests to verify the client's identity. The ycmd server also includes an
HMAC header in all responses to allow the client to verify the responses.
'''



logger = logging.getLogger('sublime-ycmd.' + __name__)


def new_hmac_secret(num_bytes=32):
    ''' Generates and returns an HMAC secret in binary encoding. '''
    hmac_secret_binary = os.urandom(num_bytes)
    return hmac_secret_binary


def _calculate_hmac(hmac_secret, data, digestmod=hashlib.sha256):
    assert isinstance(hmac_secret, (str, bytes)), \
        'hmac secret must be str or bytes: %r' % (hmac_secret)
    assert isinstance(data, (str, bytes)), \
        'data must be str or bytes: %r' % (data)

    hmac_secret = str_to_bytes(hmac_secret)

# Path: lib/util/str.py
# def bytes_to_str(data):
#     '''
#     Converts `data` to `str`.
# 
#     If data is a `bytes`, it is decoded into `str`.
#     If data is `str`, it is returned as-is.
#     '''
#     assert isinstance(data, (str, bytes)), \
#         'data must be str or bytes: %r' % (data)
#     if isinstance(data, str):
#         # already str, yay
#         return data
#     return data.decode()
# 
# def str_to_bytes(data):
#     '''
#     Converts `data` to `bytes`.
# 
#     If data is a `str`, it is encoded into `bytes`.
#     If data is `bytes`, it is returned as-is.
#     '''
#     assert isinstance(data, (str, bytes)), \
#         'data must be str or bytes: %r' % (data)
#     if isinstance(data, bytes):
#         # already bytes, yay
#         return data
#     return data.encode()
# 
# Path: lib/util/format.py
# def base64_encode(data):
#     '''
#     Encodes the given `data` in base-64. The result will either be a `str`,
#     or `bytes`, depending on the input type (same as input type).
#     '''
#     assert isinstance(data, (str, bytes)), \
#         'data must be str or bytes: %r' % (data)
# 
#     is_str = isinstance(data, str)
#     if is_str:
#         data = str_to_bytes(data)
# 
#     encoded = base64.b64encode(data)
# 
#     if is_str:
#         encoded = bytes_to_str(encoded)
# 
#     return encoded


# Path: lib/util/hmac.py
import hashlib
import hmac
import logging
import os
from ..util.str import (
    bytes_to_str,
    str_to_bytes,
)
from ..util.format import (
    base64_encode,
)

#!/usr/bin/env python3

'''
lib/util/hmac.py
Contains HMAC utility functions. The ycmd server expects an HMAC header with
all requests to verify the client's identity. The ycmd server also includes an
HMAC header in all responses to allow the client to verify the responses.
'''



logger = logging.getLogger('sublime-ycmd.' + __name__)


def new_hmac_secret(num_bytes=32):
    ''' Generates and returns an HMAC secret in binary encoding. '''
    hmac_secret_binary = os.urandom(num_bytes)
    return hmac_secret_binary


def _calculate_hmac(hmac_secret, data, digestmod=hashlib.sha256):
    assert isinstance(hmac_secret, (str, bytes)), \
        'hmac secret must be str or bytes: %r' % (hmac_secret)
    assert isinstance(data, (str, bytes)), \
        'data must be str or bytes: %r' % (data)

    hmac_secret = str_to_bytes(hmac_secret)

    data = str_to_bytes(data)