How to calculate HMAC programmatically
We are going to implement the hmac
program that will calculate HMAC-SHA-256.
Our hmac
program will need two command-line arguments:
- The input filename
- The secret key, hex-encoded
When it comes to the Application Programming Interface (API) for HMAC calculations, OpenSSL 3.0 provides three whole APIs:
- The deprecated legacy low-level API, consisting of functions with an
HMAC_
prefix. A fun fact about this API is that even though it’s not anEVP
API, it uses theEVP_MD
API to access underlying message digest functions. - The
EVP_DigestSign
API. This API is mostly used for digital signatures. It is possible to use this API for MAC, but such usage is not very intuitive. - The
EVP_MAC
API. This new API was specially created for MAC and introduced in OpenSSL 3.0. We are going to use this API in our code.
The official documentation for the EVP_MAC
API can be found on the man page of the same name:
$ man...