# Path: webtools/utils/encoding.py
# def smart_bytes(value, encoding='utf-8', errors='strict'):
#     if isinstance(value, bytes):
#         if encoding == 'utf-8':
#             return value
# 
#         return value.decode('utf-8', errors).encode(encoding, errors)
# 
#     if not isinstance(value, str):
#         value = str(value)
# 
#     return value.encode(encoding, errors)


import hmac
import struct
import hashlib
import binascii
import operator
import time
import random
    import warnings
from functools import reduce
from webtools.utils.encoding import smart_bytes
except NotImplementedError:
    warnings.warn('A secure pseudo-random number generator is not available '
                  'on your system. Falling back to Mersenne Twister.')
    using_sysrandom = False



_trans_5c = bytearray([(x ^ 0x5C) for x in range(256)])
_trans_36 = bytearray([(x ^ 0x36) for x in range(256)])


def salted_hmac(key_salt, value, secret=None):
    """
    Returns the HMAC-SHA1 of 'value', using a key generated from key_salt and a
    secret (which defaults to settings.SECRET_KEY).

    A different key_salt should be passed in for every application of HMAC.
    """
    if secret is None:
        secret = settings.SECRET_KEY

    # We need to generate a derived key from our base key.  We can do this by
    # passing the key_salt and our base key through a pseudo-random function and
    # SHA1 works nicely.
    key = hashlib.sha1((key_salt + secret).encode('utf-8')).digest()

    # If len(key_salt + secret) > sha_constructor().block_size, the above
    # line is redundant and could be replaced by key = key_salt + secret, since
    # the hmac module does the same thing for keys longer than the block size.
    # However, we need to ensure that we *always* do this.

# Path: webtools/utils/encoding.py
# def smart_bytes(value, encoding='utf-8', errors='strict'):
#     if isinstance(value, bytes):
#         if encoding == 'utf-8':
#             return value
# 
#         return value.decode('utf-8', errors).encode(encoding, errors)
# 
#     if not isinstance(value, str):
#         value = str(value)
# 
#     return value.encode(encoding, errors)


# Path: webtools/utils/crypto.py
import hmac
import struct
import hashlib
import binascii
import operator
import time
import random
    import warnings
from functools import reduce
from webtools.utils.encoding import smart_bytes

except NotImplementedError:
    warnings.warn('A secure pseudo-random number generator is not available '
                  'on your system. Falling back to Mersenne Twister.')
    using_sysrandom = False



_trans_5c = bytearray([(x ^ 0x5C) for x in range(256)])
_trans_36 = bytearray([(x ^ 0x36) for x in range(256)])


def salted_hmac(key_salt, value, secret=None):
    """
    Returns the HMAC-SHA1 of 'value', using a key generated from key_salt and a
    secret (which defaults to settings.SECRET_KEY).

    A different key_salt should be passed in for every application of HMAC.
    """
    if secret is None:
        secret = settings.SECRET_KEY

    # We need to generate a derived key from our base key.  We can do this by
    # passing the key_salt and our base key through a pseudo-random function and
    # SHA1 works nicely.
    key = hashlib.sha1((key_salt + secret).encode('utf-8')).digest()

    # If len(key_salt + secret) > sha_constructor().block_size, the above
    # line is redundant and could be replaced by key = key_salt + secret, since
    # the hmac module does the same thing for keys longer than the block size.
    # However, we need to ensure that we *always* do this.

    return hmac.new(key, msg=smart_bytes(value), digestmod=hashlib.sha1)