How to calculate HMAC on the command line
Now we will learn how to calculate HMAC on the command line. To do this, we need to instruct the openssl
tool on which type of MAC function to run and which underlying function to use. The MAC function will be HMAC
, and for the underlying function, let’s choose the SHA-256
hash function.
The openssl
tool provides two methods of HMAC calculation:
- Using the
openssl dgst
subcommand. This is the same subcommand that we used to calculate a message digest in the previous chapter. - Using the
openssl mac
subcommand. A new subcommand was introduced in OpenSSL 3.0.
We will learn how to use both methods.
As usual, first, let’s generate a sample file that we will use as input for HMAC calculation:
$ seq 20000 >somefile.txt
HMAC calculation requires a secret key. So, let’s generate it:
$ openssl rand -hex 32 df036c471b612f8ad099078d8e3bd9c64339e7aeab56ec75e2222c415db113de
Here is how you calculate...