import hashlib
import random


def generate_sha1(string, salt=None):
    """
    Generates a sha1 hash for supplied string. Doesn't need to be very secure
    because it's not used for password checking. We got Django for that.

    :param string:
        The string that needs to be encrypted.

    :param salt:
        Optionally define your own salt. If none is supplied, will use a
        random string of 5 characters.

    :return: Tuple containing the salt and hash.

    """
    if not salt:
        salt = hashlib.sha1(  # noqa
            str(random.random()).encode("utf-8")  # noqa
        ).hexdigest()[:5]
    key = hashlib.sha1(
        (salt + str(string)).encode("utf-8")
    ).hexdigest()  # noqa

    return (salt, key)