import hashlib
from typing import OrderedDict


def compute_identity(sample, size):
    """Compute a unique hash out of a dictionary

    Parameters
    ----------
    sample: dict
        Dictionary to compute the hash from

    size: int
        size of the unique hash
    """
    sample_hash = hashlib.sha256()

    for k, v in sorted(sample.items()):
        sample_hash.update(k.encode('utf8'))

        if isinstance(v, (dict, OrderedDict)):
            sample_hash.update(compute_identity(v, size).encode('utf8'))
        else:
            sample_hash.update(str(v).encode('utf8'))

    return sample_hash.hexdigest()[:size]