import time
import hashlib


def write_str_to_file(string, extension='', encoding='utf-8'):
    """
    Save the contents of a string to a file.

    :param string: the data to be saved
    :param extension: the file extension to be used for the file's name
    :returns the name of the file containing the string data
    """

    # Convert the code to a byte string.
    blob = string.encode(encoding)

    # Use the time so that two identical files have different file names
    time_bytes = bytes(str(time.time()), encoding=encoding)

    # Hash the byte string to generate a filename.
    m = hashlib.sha1()
    m.update(blob)
    m.update(time_bytes)
    hash_code = m.hexdigest()

    filename = "{}{}".format(hash_code, extension)

    f = open(filename, 'w')
    f.write(string)
    f.close()

    return filename