HMAC
HMAC (Hash-based Message Authentication Code) uses a secret key so only authorized parties can generate a valid code for a message.
A plain hash shows that data has not changed by accident, but an attacker who changes the data on purpose can simply compute a new hash to go with it. An HMAC fixes this by mixing a secret key into the hash. The sender computes the code from the message and the key, and sends the code alongside the message. The receiver, who knows the same key, computes the code again and compares the two. Without the key, an attacker who alters the message cannot produce a matching code.
You will find HMACs wherever two parties share a secret and need to trust each other's messages:
signed session cookies, JSON Web Tokens signed with HS256, API request
signing, and webhook signatures from services like GitHub and Stripe. (A
digital signature does a similar job with a key pair, so the
receiver can check a message without being able to sign one.)
Two details matter when you check an HMAC. First, use your language's built-in HMAC function rather
than hashing the key and message together yourself, since naive constructions such as
sha256(key + message) are open to length extension attacks. Second, compare codes with a
constant-time function, such as Python's hmac.compare_digest(), so an attacker cannot work out a
valid code from how long each comparison takes.