import hashlib


def get_metadata_hash_for_attachments(attachments):
    """
    Calculate a metadata hash from a collection of attachments.

    The hash will change if any of the attachments changes.
    """
    hashes = [attachment.metadata_hash for attachment in attachments]
    # Sort the hashes to make the hash deterministic regardless of order
    hashes.sort()

    data = b"".join([hash_.encode("utf-8") for hash_ in hashes])

    return hashlib.sha256(data).hexdigest()