from typing import Union
from pathlib import Path
import hashlib


def md5_for_file(file_path: Union[Path, str], block_size: int = 8192) -> str:
    """Get md5 hex digest for a file.
    :param file_path: Path to input file
    :param block_size: Input block size. Should be multiple of 128 bytes.

    :returns: md5 hex digest
    """

    _path = Path(file_path)
    if not _path.is_file():
        raise ValueError(f"Provided file path is invalid: {file_path}")

    if not (block_size >= 0 and not block_size % 128):
        raise ValueError(f"Provided block_size is invalid: {block_size}")

    md5 = hashlib.md5()
    with open(_path, 'rb') as f:
        while True:
            data = f.read(block_size)
            if not data:
                break
            md5.update(data)

    return md5.hexdigest()