What Is HMAC Used For
Prateek Zare

Written by Prateek Zare

Software Developer with ML and Data Expertise, 8+ years of experience

Last updated

What Is HMAC Used For? Why APIs Need Signed Requests

What is HMAC used for? HMAC, hash based message authentication code, is used to prove a request or payload came from someone holding a shared secret key and was not altered in transit. A plain hash only detects accidental corruption and can be recomputed by anyone, since it uses no secret at all. That difference is exactly why APIs, webhooks, and signed URLs rely on HMAC instead of a bare hash whenever they need to trust the source of a message, not just check that the bytes arrived intact.

Quick answer

HMAC, hash based message authentication code, is used to prove a request or payload came from someone holding a shared secret key and was not altered in transit. A plain hash only detects accidental corruption and can be recomputed by anyone, since it uses no secret at all.

A plain hash and an HMAC solve different problems

A plain hash function, like SHA256 on its own, takes any input and produces a fixed length digest. Change one character in the input and the digest changes completely. That property makes plain hashes great for checking that a file downloaded correctly, or that two copies of a document are identical. You can compute a hash with our hash generator and compare it against a published checksum in seconds.

The problem is that a plain hash carries no secret. Anyone, including an attacker sitting between a client and a server, can take a payload, run it through SHA256, and produce the exact same digest you would. So if a server only checks "does the hash match," it has confirmed the payload was not corrupted by a network glitch, but it has confirmed nothing about who sent it. An attacker who intercepts a request, changes the amount field in a payment, and recomputes the hash will produce a digest that still matches the tampered body perfectly.

Where the secret key changes everything

HMAC fixes this by mixing a shared secret key into the hashing process itself. Instead of hashing just the message, HMAC combines the key and the message through two nested hash operations, roughly hash(key XOR opad, hash(key XOR ipad, message)) using whichever underlying hash function you choose, commonly SHA256. The output, called a signature or a tag, depends on both the message and the key. Change either one and the signature is different.

This is what HMAC is used for in practice: only someone who knows the secret key can produce a signature that the receiving server will accept. An attacker who intercepts a request can still read it, and can still change it, but cannot forge a valid signature for the altered version without the key. The server recomputes the expected signature using its own copy of the secret and compares it against the one attached to the request. If they match, the server trusts both that the sender knows the secret and that the payload was not modified after signing.

What is HMAC used for? Generate and check signatures directly

Understanding HMAC on paper is one thing, testing it against real webhook payloads is another. Our free HMAC Generator lets you paste a message and a secret key, choose a hash algorithm like SHA256, and get the signature instantly, so you can confirm your server side verification logic matches what a provider actually sends.

Open the HMAC Generator

A concrete example: verifying a webhook from a payment provider

Most payment processors send webhooks to notify your server about events like a completed charge or a refund. Because these webhooks trigger real actions, such as marking an order paid or releasing a shipment, you cannot just trust whatever hits your endpoint. Anyone who guesses your webhook URL could send a fake "payment succeeded" event otherwise.

To prevent that, the provider signs each webhook body with HMAC using a secret only your account and the provider know. The request typically arrives with a header like this:

X_SIGNATURE: t=1700000000, v1=5257a869e7...

Your server takes the raw request body, runs it through HMAC SHA256 with your webhook secret, and checks the result against the v1 value in the header. If they match, the event is genuine and unaltered, so you process it. If they do not match, you reject the request outright, no matter how legitimate the payload looks. This single check is what stops a forged "refund issued" event from draining your ledger.

Constant time comparison matters too

One detail teams get wrong is comparing signatures with a normal string equality check. A naive comparison can exit early on the first mismatched byte, and the tiny timing difference between failing at byte one versus byte thirty can, in theory, leak information to an attacker probing many requests. Proper implementations use a constant time comparison function so every check takes the same amount of time regardless of where the mismatch occurs. Most standard libraries ship one specifically for this purpose, so use it rather than writing your own comparison loop.

HMAC compared to other things that look similar

It helps to separate HMAC from a few adjacent concepts. A JSON Web Token, which you can inspect with our JWT decoder, often uses HMAC internally as one of its signing algorithms, specifically HS256. But a JWT is a container format for claims, while HMAC is the signing mechanism that protects the token from tampering. You can have HMAC without JWT, such as signing a webhook body directly, and JWT without HMAC, if the token uses RSA or ECDSA signatures instead.

Base64 encoding is another thing people sometimes confuse with security. Encoding a signature with our base64 encoder just changes how bytes are represented as text so they survive transport in a URL or header. It adds no secrecy and no integrity guarantee on its own. HMAC provides the actual protection; base64 is just packaging.

Password hashing is a separate case again. When you store user passwords, you want a slow, salted hash designed to resist brute force, not HMAC. Our password strength checker is useful for the input side of that problem, but the storage mechanism itself is a different topic from request signing entirely.

Why not just encrypt the request instead

Encryption and HMAC solve different problems too. Encryption keeps content secret from anyone without the decryption key. HMAC does not hide anything, since the message can travel in plain text alongside its signature. What HMAC guarantees is authenticity and integrity: this message came from someone with the key, and it has not changed since they signed it. Many systems run over HTTPS, which already provides transport encryption, and then add HMAC on top so the application layer itself can verify sender identity even if something further up the chain terminates or re originates the connection. The two mechanisms are complementary, not substitutes for each other.

The formal definition of HMAC comes from RFC 2104, published by the IETF, which specifies exactly how the key and message get combined with the underlying hash function. If you want the precise construction rather than a summary, that document is the primary source.

Tools that pair with HMAC

HMAC in one paragraph

A plain hash proves a message was not corrupted, but anyone can compute one, so it proves nothing about who sent it. HMAC mixes a shared secret key into the hash so only someone holding that key can produce a valid signature, which is what HMAC does at its core: authenticating the sender and protecting the payload from tampering at the same time. Next time you need to verify a webhook or sign an API request, run it through the HMAC Generator above and confirm your signature matches before you trust the payload.

What is HMAC used for beyond webhooks?

A clear way to ask what is HMAC used for is to look at who needs trust. The post above shows one answer, and there are several more. What is HMAC used for in every case? The pattern is always the same: two sides share a secret and need proof that a message is theirs.

What is HMAC used for in real systems?

Here is a map. Read it as an answer to what is HMAC used for in five common places.

SystemWhat is HMAC used for thereTypical hash
Cloud API requestsWhat is HMAC used for in a signed API call: proving the caller holds the secret keySHA256
WebhooksWhat is HMAC used for in a webhook: proving the provider sent the bodySHA256
JWT tokens with HS256What is HMAC used for in a token: signing the header and payloadSHA256
One time passcodesWhat is HMAC used for in an authenticator app: turning a counter or time into a codeSHA1 by default
Key derivationWhat is HMAC used for in password hashing schemes like PBKDF2: mixing the password into each roundVaries

Notice what is HMAC used for in each row. The job never changes: prove the sender knows the secret.

AWS signed requests, HS256 tokens and the codes in an authenticator app all rest on the same primitive. That is the short version of what is HMAC used for across the industry.

What is HMAC used for when you stop replay attacks?

A valid signature proves who sent a message. It does not prove when. An attacker can record a genuine request and send it again. So many providers sign a timestamp together with the body. Your server checks the signature and then rejects anything older than a short window, often five minutes. Add a unique request id if you need exact once delivery. So what is HMAC used for here? Binding a message to a moment.

How long is the output when you ask what is HMAC used for?

The length depends on the hash, not on the message or the key. HMAC SHA256 always gives 32 bytes, shown as 64 hex characters. HMAC SHA1 gives 20 bytes, or 40 hex characters. A one byte message and a one gigabyte message produce the same length. Knowing this helps when you store signatures. What is HMAC used for in a database column? Audit trails, and you can size the field once.

Which mistakes break what is HMAC used for in your design?

  • Signing a parsed object instead of the raw body. Re serialising changes the bytes, so what is HMAC used for if the signature always fails?
  • Using a short or guessable secret. A weak key defeats the point however strong the hash.
  • Comparing signatures with a plain equals check. What is HMAC used for if the compare leaks timing? Very little, so use a constant time function.
  • Putting the secret in client side code. Anyone can read it, so what is HMAC used for there? Nothing, the signature proves no one.
  • Using the same secret for every customer or environment. Rotate keys, because what is HMAC used for without a fresh secret after a leak?

Quick answers: what is HMAC used for by role?

Different readers ask what is HMAC used for with different goals in mind.

  • Backend developer: what is HMAC used for in my API? Verifying that each request came from a client holding the key.
  • Payments engineer: what is HMAC used for with provider events? Rejecting forged webhook calls before they touch your ledger.
  • Mobile developer: what is HMAC used for in an app? Signing requests, though the key must live on a server, never in the app.
  • Security reviewer: what is HMAC used for in tokens? Letting a server trust its own HS256 tokens without a database lookup.
  • Student: what is HMAC used for in class? A clean example of a keyed hash that a plain hash cannot replace.

Want to see it in action? Paste a message and a key into our HMAC generator and watch the signature change when you edit one character. For the bigger picture, read how hashing differs from encryption. Both help answer what is HMAC used for in plain terms. If you remember one line, remember this: what is HMAC used for is proving a message came from a key holder.

In this guide
A plain hash and an HMAC solve different problems
A concrete example: verifying a webhook from a payment provider
HMAC compared to other things that look similar
Why not just encrypt the request instead
What is HMAC used for? Generate and check signatures directly

FAQ: What Is HMAC Used For?

What is HMAC used for in one sentence?

HMAC is used to prove that a message or request came from someone holding a shared secret key and was not altered after it was signed, which a plain hash cannot do on its own.

What is the difference between a hash and an HMAC?

A plain hash uses no secret, so anyone can recompute it from the same input. HMAC combines a secret key with the message before hashing, so only someone with the key can produce a signature that will be accepted.

Why can't a plain hash verify who sent an API request?

Because a plain hash is fully public math. An attacker can intercept a request, change the payload, recompute the same hash function, and produce a digest that matches perfectly, since there is no secret involved to stop them.

How does a payment provider use HMAC to secure webhooks?

The provider signs each webhook body with HMAC using a secret shared with your account, then sends the signature in a header. Your server recomputes the signature with its own copy of the secret and rejects the request if the values do not match.

Does HMAC encrypt the data being sent?

No. HMAC does not hide the message content at all. It only proves authenticity and integrity. If you also need confidentiality, that comes from transport encryption such as HTTPS, used alongside HMAC rather than instead of it.

Is a JSON Web Token the same thing as HMAC?

No. A JSON Web Token is a container format for claims that can use HMAC, specifically the HS256 algorithm, as one option for signing it. HMAC itself is just the signing mechanism, not the token format.

Why should signature comparisons be constant time?

A naive string comparison can exit as soon as it hits a mismatched byte, and the resulting timing difference can leak information to an attacker sending many probe requests. A constant time comparison takes the same time regardless of where a mismatch occurs, closing that gap.

Which hash function does HMAC typically use?

HMAC can wrap almost any cryptographic hash function, but SHA256 is the most common choice today for APIs and webhooks, referred to as HMAC SHA256 or HMAC dash SHA256 in documentation.