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 https://github.com/algorand/py-algorand-sdk/blob/833cc463b10571e1e816b6db786a3c9c202f4816/algosdk/transaction.py#L1417 where the TxGroup class is canonically serialized.
    b. Domain separation is a general good practice. See https://github.com/algorand/py-algorand-sdk/blob/833cc463b10571e1e816b6db786a3c9c202f4816/algosdk/transaction.py#L1418 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 https://github.com/algorand/py-algorand-sdk/blob/833cc463b10571e1e816b6db786a3c9c202f4816/algosdk/transaction.py#L1419 and https://github.com/algorand/py-algorand-sdk/blob/833cc463b10571e1e816b6db786a3c9c202f4816/algosdk/encoding.py#L185

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