import hashlib


def hash_file(fpath, algorithm="sha256", chunk_size=65535):
    """
    Calculates a file hash.

    Parameters
    ------------
    fpath: str
        Path to the file being validated
    algorithm: str
        Hash algorithm. One of the following: "sha256", "sha512", "sha1", "md5"
    chunk_size: int
        Bytes to read at a time

    Returns
    ----------
    file_hash: str
        The resulting file hash.

    Returns
    -------
    file_hash: str
        Resulting file hash
    """
    hasher_map = {
        "sha256": hashlib.sha256,
        "sha512": hashlib.sha512,
        "sha1": hashlib.sha1,
        "md5": hashlib.md5
    }

    try:
        hasher = hasher_map[algorithm]()
    except KeyError:
        raise ValueError("Algorithm {} not supported. Available: {}".format(algorithm, list(hasher_map.keys())))

    with open(fpath, "rb") as fp:
        bytes_read = fp.read(chunk_size)
        while len(bytes_read) > 0:
            hasher.update(bytes_read)
            bytes_read = fp.read(chunk_size)

    return hasher.hexdigest()