import hashlib
import hmac


def _salted_hmac(key_salt, value, secret):
    """
    Returns the HMAC-SHA1 of 'value', using a key generated from key_salt and a secret.

    A different key_salt should be passed in for every application of HMAC.
    Lifted from: http://code.djangoproject.com/svn/django/trunk/django/utils/crypto.py
    """
    # 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).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=value, digestmod=hashlib.sha1)