How to convert a dictionary to 32-byte hash

In the transaction field, I want to convert a dictionary with several objects into a 32-byte hash to use in the metadata field. How can I achieve this?

You have two steps to do:

  1. Serialize your dictionary into some byte string.
  2. Hash it using some cryptographically secure hash function with a 32-byte output.

There are many ways to do each step.
Here is the way it is done in other parts of the Algorand code:

  1. Serialization is done using a canonical version of msgpack. Then the resulting bitstring is prepended with some unique prefix for domain separation.
    a. Canonical means that if you serialize the dictionary twice in two different machines, you obtain the same byte representation. This is a good feature. See where the TxGroup class is canonically serialized.
    b. Domain separation is a general good practice. See where the prefix constants.tgid_prefix=b"TG" is used. Personally, I use prefix like **abcd to be sure to not collide with official Algorand prefixes.
  2. Hashing uses SHA512-256. See and

Thank you Fabrice. I’ll go over those links and revert.