import time
import hashlib


def calculate_key_iteration_count(seconds=1):
    """
    Calculate the amount of iterations that can be performed in the given
    timespan.

    This amount can be used to determine the amount of iterations
    used for key derivation.

    :return: Key iteration count
    :rtype: int
    """
    TEST_ITERATION_COUNT = 1000000
    start = time.time()

    hashlib.pbkdf2_hmac(
        hash_name="sha256",
        password=b"2"*32,
        salt=b"1"*16,
        iterations=TEST_ITERATION_COUNT
    )

    end = time.time()

    iteration_count = int((TEST_ITERATION_COUNT / (end - start)) * seconds)

    # Ensure the minimum iteration count is always at least 100,000 in case
    # an extremely low count was calculated
    return max(100000, iteration_count)