$linuxjunkies
>

HMAC

also: Hash-based Message Authentication Code

HMAC (Hash-based Message Authentication Code) is a cryptographic technique that combines a secret key with a hash function to verify both the authenticity and integrity of a message.

HMAC uses a shared secret key along with a hash algorithm (like SHA-256) to produce a fixed-length code that proves a message hasn't been tampered with and came from someone with the secret key. Unlike plain hashing, an attacker cannot forge an HMAC without knowing the secret key.

The process works by mixing the secret key into the hash computation using a specific algorithm, then verifying the HMAC on the receiving end by computing it again with the same key. If the HMACs match, the message is authentic and unchanged.

Common use cases include authenticating API requests, signing cookies in web applications, and verifying SSH key exchanges. For example, you might use openssl dgst -sha256 -hmac 'secretkey' myfile.txt to generate an HMAC for a file.

Related terms