Join our Newsletter — 33% off our NHI Course
Home› Guides› HMAC for Beginners: How a Shared Secret Proves…
Guide Web & API Security

HMAC for Beginners: How a Shared Secret Proves a Message Was Not Changed

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 28 November 2026 12 min read
On this page

HMAC is a way to stamp a message with a shared secret, so the receiver can tell it is genuine.

This guide is for students, people new to coding and anyone who has seen "HMAC" in an app's settings or a security course. You do not need to write code to be affected: payment services, cloud platforms and the code app on your phone all use it behind the scenes.

In short

  • HMAC mixes a message with a secret that only the sender and receiver know, and produces a short code called a tag.
  • The receiver makes the tag again with its own copy of the secret. If the two tags match, the message is genuine and unchanged.
  • HMAC does not hide the message. It proves who made it and that nobody changed it.
  • The whole method depends on the secret, so keep it out of code and chat, and change it if it leaks.

What you will learn

  • Explain what HMAC does in one sentence.
  • Spot the difference between a plain hash, an HMAC and a digital signature.
  • Check where the HMAC secrets in your apps are kept.
  • Ask whether your apps check the tag on every message they receive.
Shared secret
a random value that two apps both know and nobody else should.
Tamper check code (tag)
the short code HMAC makes from a message and the secret.
Fingerprint of data (hash)
a short summary of data that changes if the data changes.

What is HMAC?

Think of two offices that each own a matching secret stamp. Nobody else has one. When a letter arrives with the right stamp mark, the receiving office knows it came from the other office.

HMAC works in a similar way for software. The name stands for hash-based message authentication code. It takes two things: the message and a shared secret. It runs them through a hash, a maths recipe that turns any data into a short fingerprint. The result is a short code, called a tag.[1]

The sender attaches the tag to the message. The receiver does the same sum with its own copy of the secret. If its answer matches the tag, two things are likely true. The message came from someone holding the secret, and nobody changed it on the way.[1][2]

Where the comparison stops working: a rubber stamp leaves the same mark on every letter, so a thief could copy it onto a new letter. An HMAC tag is different for every message. Change one letter of the message and the tag no longer matches. Without the secret, a stranger should not be able to make a new tag that fits.

Why does it matter?

Apps send each other messages all day: "this customer paid", "add this file", "start this job". If an app acts on any message that arrives, a stranger can send fake ones. HMAC gives the receiving app a quick way to say no.

HMAC secrets are linked to non-human identities (NHIs). A non-human identity is a digital identity, or the credential behind it, that represents a machine, application, automated process, service or AI agent. The HMAC secret is one of those credentials. Whoever holds it can make messages that look genuine.

If you are new to coding

What could happen: you paste the secret into your code to test it, and the code ends up in a shared project.

Check this: read the secret from secure settings, and use the service's own library to check tags.

Next: Secrets Management for Beginners

If your business connects apps together

What could happen: your shop accepts a fake "payment received" message because nothing checks the tag.

Check this: ask your developer or supplier whether every incoming message is checked before the app acts on it.

Next: Webhooks for Beginners

If you are studying for a security exam

What could happen: you mix up hashing, HMAC and digital signatures, which exams often test side by side.

Check this: for each one, ask who can make it and who can check it.

Next: Digital Signatures for Beginners

What does it look like?

Here is a made-up message from a ticket app to a payment partner. The tag travels in a header, which is a labelled line sent along with the message.

Example only. All values are made up.

Sent to: https://api.example.invalid/orders
X-Timestamp: 0000000000                      (1)
X-Signature: sha256=FAKE-TAG-0000-EXAMPLE    (2)

{
  "order": "EXAMPLE-ORDER-0000",             (3)
  "seats": 2
}
  1. The time the message was made. It is often included in the sum, so an old message can be spotted.
  2. In a real message, this line would contain a long string of letters and numbers: the HMAC tag.
  3. The message itself. Anyone can read it, because HMAC does not hide the content.

Why this is risky: the message is in plain view. If the receiver skips the tag check, a stranger could send the same format with different numbers.

The safer way: the receiver makes the tag again from the message, the time and its copy of the secret, then compares. GitHub's guide says to use a special comparison that takes the same time whatever the result, not a plain "equals" check.[3]

How does it work?

Here is what happens each time one app sends an HMAC-protected message to another.

  1. Share a secret once. The two sides agree a long, random secret. GitHub advises a random string with high entropy, meaning hard to guess.[3] The original design also says keys should be changed from time to time.[1]
  2. Make the tag. The sender feeds the message and the secret into HMAC. Most services today use it with a hash called SHA-256.[3][4]
  3. Send. The message and the tag travel together, while the secret itself stays behind on each side.
  4. Check. The receiver makes its own tag and compares. If they match, it goes ahead, and if not, it rejects the message.
  5. Check the time. Many services also refuse old messages. AWS, for example, usually rejects a signed request that arrives more than five minutes after its time stamp.[5]
Diagram titled How HMAC checks a message. On the left, a box labelled Sender holds the message and a copy of the shared secret, which go into a box labelled HMAC that produces a tag. An arrow labelled message plus tag, the secret stays home, goes right to a box labelled Receiver, which also holds its own copy of the shared secret. The receiver puts the message and its secret into HMAC and gets its own tag. A compare box leads to two outcomes: a green arrow labelled tags match leads to Accept, and a dark arrow labelled tags differ leads to Reject and log. A dashed box at the bottom left asks: changed on the way? One changed letter gives a different tag, so the check fails.
Figure 1. Both sides hold the secret. Only the message and the tag travel, and the receiver checks the tag before it acts.

HMAC is easy to confuse with two close cousins. A plain hash has no secret, so anyone can make one. A digital signature uses a pair of keys: a private key to sign and a public key that anyone can use to check. HMAC sits in the middle. It needs a secret, but both sides hold the same one.

Diagram titled Hash, HMAC and digital signature compared. Three columns. Column one, Plain hash: no secret; who can make it: anyone; who can check it: anyone; good for: spotting accidental changes, such as a damaged download. Column two, HMAC: one shared secret held by both sides; who can make it: anyone with the secret; who can check it: anyone with the secret; good for: two apps that trust each other, such as webhooks and API requests. Column three, Digital signature: a private key and a public key; who can make it: only the private key holder; who can check it: anyone with the public key; good for: proof to the wider world, such as signed software. A green bar at the bottom says: ask who can make it and who can check it.
Figure 2. The quick test: who can make the code, and who can check it?

You meet HMAC in more places than you might think. GitHub signs its webhook messages with it.[3] AWS uses it to sign requests made with access keys.[4] The six-digit codes in many authenticator apps are also built on HMAC, mixed with the current time.[6]

A worked example

Jana Achterberg has just started as a junior developer at Lindenfeld Tickets, a fictional firm that sells concert seats. Her first task is to accept "payment complete" messages from the firm's payment partner.

The partner's guide says every message carries an HMAC tag. Jana's first version simply reads the message and marks the seats as paid. It works in testing, so she asks her team lead, Sana Bergmann, to review it.

Sana asks one question: "What stops someone else sending this message?" Jana thinks of the matching office stamps. Her code never looks at the stamp.

She adds the check. Her code now rebuilds the tag from the message, the time stamp and the shared secret, then compares it using the partner's own library. Messages with a wrong tag or an old time stamp are rejected and logged.

During review, Sana spots something else. The secret is typed into a settings file inside the project. Jana moves it to the hosting service's secure settings. Because the old value has been seen in the project history, the team asks the partner for a new secret and retires the old one.

A week later, the logs show a handful of rejected messages with tags that do not match. No seats were marked as paid from them, according to the records available to the team.

Myths and mistakes

What people thinkWhat is true
HMAC encrypts the message.It does not. Anyone who sees the message can read it. HMAC only shows it is genuine and unchanged. Use HTTPS or encryption to keep it private.
A hash of the message is just as good.A plain hash has no secret, so an attacker can change the message and make a new hash. HMAC needs the secret.[1]
HMAC proves exactly which person sent it.It proves someone holding the secret made it. Both sides hold the secret, so either could have made the tag. Digital signatures are used when that matters.
Any secret will do.Short or guessable secrets weaken it. Use a long, random secret.[1][3]
A valid tag means the message is new.A copied message keeps its valid tag. Check the time stamp and skip messages you have already handled.

What can I do this week?

  • List the services that send signed messages to your apps.
  • Ask your developer or supplier whether each app checks the tag and the time stamp.
  • Search your shared code and chat for HMAC or signing secrets.
  • Move any secret you find into secure settings, then ask for a new one.
  • Give each secret a named owner and a date to change it.

Checklist

  • Every incoming signed message is checked before the app acts on it.
  • Checks use the service's own library or a time-safe comparison.
  • Old messages are rejected using the time stamp.
  • Repeated messages are spotted and skipped.
  • Secrets are long and random, and live in secure settings.
  • Each secret has an owner and is changed on a schedule.
  • A leaked secret is replaced straight away.
  • Messages that also need privacy travel over HTTPS.

Common questions

What does HMAC stand for?

Hash-based message authentication code. "Hash" is the maths recipe it uses inside. "Message authentication code" means a short code that shows a message is genuine. You will often see the hash named too, as in HMAC-SHA256, which means HMAC built on the SHA-256 hash.[1]

Is HMAC the same as a digital signature?

No. Both show a message is genuine, but they work differently. HMAC uses one secret that both sides share. A digital signature uses a private key to sign and a public key to check. So anyone can check it, but only the key holder can make one.

Why do some apps call the HMAC tag a signature?

It is common shorthand. GitHub, for example, sends its HMAC tag in a header with "Signature" in the name.[3] Strictly speaking it is a message authentication code, made with a shared secret, not a public key signature. The everyday meaning is the same: a check that the message is genuine.

What should I do if an HMAC secret leaks?

Ask the other service for a new secret straight away, update your app, then retire the old one. Until it is retired, anyone holding the old secret may be able to make messages that pass the check. Then look through recent records for messages you did not expect.

Is HMAC still safe to use?

Yes, when used well. Its strength depends on the hash inside it and on the secret.[1] NIST, the US standards body, publishes it as a standard.[2] Problems usually come from weak or leaked secrets, or from apps that skip the check.

Where next?

Ready for the detail? Read the practitioner guide: Webhook HMAC Signing vs mTLS vs IP Allowlisting. It goes further into how HMAC compares with certificate checks and address lists, how replays are stopped, and how to layer the three.

Also useful:

Related NHI Mgmt Group resources: Non-Human Identities for Beginners · Secrets Management for Beginners · API Security Guide

Key terms

HMAC
A method that mixes a message with a shared secret to make a code that shows the message is genuine and unchanged.
Message authentication code
A short code that shows a message was not changed and was made by someone who knows the secret.
Hash function
A maths recipe that turns any data into a short, fixed-length fingerprint.
Shared secret
A value known to both sides of a connection, which must be kept from everyone else.
Integrity
Confidence that information has not been changed by someone who should not change it.
Digital signature
A code made with a private key that anyone with the matching public key can check.
Replay attack
Sending a genuine, signed message again later to trigger the same action twice.

Sources and further reading

  1. RFC 2104: HMAC: Keyed-Hashing for Message Authentication. H. Krawczyk, M. Bellare and R. Canetti, IETF, February 1997. https://www.rfc-editor.org/rfc/rfc2104, accessed 28 Nov 2026.
  2. FIPS 198-1: The Keyed-Hash Message Authentication Code (HMAC). NIST, July 2008. https://csrc.nist.gov/pubs/fips/198-1/final, accessed 28 Nov 2026.
  3. Validating webhook deliveries. GitHub Docs, GitHub. https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries, accessed 28 Nov 2026.
  4. Create a signed AWS API request. AWS Identity and Access Management User Guide, Amazon Web Services. https://docs.aws.amazon.com/IAM/latest/UserGuide/create-signed-request.html, accessed 28 Nov 2026.
  5. AWS Signature Version 4 for API requests. AWS Identity and Access Management User Guide, Amazon Web Services. https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html, accessed 28 Nov 2026.
  6. RFC 6238: TOTP: Time-Based One-Time Password Algorithm. D. M'Raihi and others, IETF, May 2011. https://www.rfc-editor.org/rfc/rfc6238, accessed 28 Nov 2026.

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.