You want to provide integrity for messages in such a way that people with a secret key can verify that the message has not changed since the integrity value (often called a tag) was first calculated.
Use a message integrity check. As with hash functions, there are somewhat standard interfaces, particularly an incremental interface.
Libraries that support MACs tend to support incremental operation using a standard structure, very similar to that used by hash functions:
Allocate and key a context object. The context object holds the internal state of the MAC until data processing is complete. The type of the context object can be specific to the MAC, or there can be a single type that works for all hash functions in a library. OpenSSL supports only one MAC and has only the associated context type. The key can be reused numerous times without reallocating. Often, you will need to specify the underlying algorithm you are using for your MAC.
Reset the context object, setting the internal parameters of the MAC to their initial state so that another message’s authentication tag can be calculated. Many MACs accept a nonce, and this is where you would pass that in. This is often combined with the “init” call when the algorithm does not take a nonce, such as with OMAC and HMAC.
“Update” the context object by passing in data to be authenticated and the associated length of that input. The results of the MAC’ing process will be dependent ...