Hashing Fundamentals

HMAC and Message Authentication

A hash can summarize a message, but by itself it cannot tell you who created that message—or whether someone changed it along the way. HMAC adds a shared secret key to the process, creating a check that helps answer both questions. It is a practical example of hashing doing more than producing a digital fingerprint: paired with carefully managed key material, it can help authenticate messages without encrypting them.

In this guide, we’ll unpack what a keyed-hash message authentication code (HMAC) does, how sender and recipient use it, and what its limits mean in everyday security. No cryptography background required—just one important distinction to keep in mind: authentication is not the same as secrecy.

What is HMAC?

HMAC stands for keyed-hash message authentication code. It combines a cryptographic hash function with a secret key to produce a short value called an authentication tag. The message and key both contribute to that tag.

A recipient who has the same secret key can calculate a tag for the received message and compare it with the tag that arrived. If the tags match, that supports the conclusion that the message has not changed since the tag was created and that it was produced by someone with access to the key.

That is useful evidence, not magic. HMAC does not identify a particular person when several people or systems share the same key. It also does not prove that a message is truthful, safe, or fresh. It checks a specific security property: message integrity and authentication among parties that share a secret.

A quick distinction: hash, HMAC, and encryption

These tools are sometimes mentioned together, but they answer different questions:

Tool What it helps answer Does it hide the message?
Ordinary hash Does this data produce the expected digest? No
HMAC Does this message match a tag made with the shared secret key? No
Encryption Can the message be read only by someone with the right decryption key? Yes, when properly used

A plain hash can help detect a change when its reference value is trusted, but anyone who can replace both the message and its unkeyed hash can create a new matching pair. HMAC makes that harder for an outsider who does not know the secret key. Encryption, by contrast, is designed to protect confidentiality. Some systems need both confidentiality and authentication, using well-designed authenticated encryption rather than expecting HMAC alone to do both jobs.

How sender and recipient use an authentication tag

Imagine two services that have securely established the same secret key. One service wants to send a message to the other. The process is straightforward in concept:

  1. The sender prepares the message. This could be a request, a data record, or another piece of information that needs an integrity check.
  2. The sender calculates an HMAC. A specified hash function and the shared secret key are used to produce a tag for the message.
  3. The sender transmits the message and tag. The tag is not a secret in the same way the key is; it must be available to the recipient for verification.
  4. The recipient checks the tag. Using the same key, hash function, and message, the recipient calculates the expected HMAC and compares it with the received tag.
  5. The recipient accepts or rejects the check. A valid match supports the message’s integrity and origin from someone who knows the key. A mismatch means the check failed, so the message should not be treated as authenticated.

Real protocols also need to define exactly which bytes are authenticated, how data is encoded, how tags are represented, and how errors are handled. Small inconsistencies in those details can undermine an otherwise sound cryptographic design.

Why combine a key with a hash?

A hash function takes data of varying lengths and produces a fixed-length digest. Useful cryptographic hashes are designed so that small changes to the input produce very different outputs, and it should be difficult to find two different inputs with the same digest. But a regular hash does not use a secret. Anyone can calculate one.

HMAC uses the key in a structured construction around the hash function. In simplified terms, the design combines the key with the message through two hash operations, using different internal padding values. That structure matters: it is not simply “hash the key and message together.” HMAC is a carefully specified method intended to use hash functions safely for message authentication.

For an accessible overview of the underlying concepts, see How Hash Functions Work and Hashing Basics.

A little history: from a cryptographic need to a common building block

HMAC was designed to provide message authentication using existing hash functions, rather than requiring a wholly separate family of hash algorithms. The construction was formalized in RFC 2104, published in 1997 by Hugo Krawczyk, Mihir Bellare, and Ran Canetti. Its arrival reflects a practical theme in security engineering: useful building blocks need not only strong mathematics, but also clear specifications that different systems can implement consistently.

Since then, HMAC has appeared in many protocols and software systems. Its familiarity is one reason it remains useful—but a name that appears in a product or configuration is not, by itself, proof that the overall system is secure. The surrounding protocol, key generation and storage, algorithm choice, and implementation all matter.

At Hashens, we treat these mechanisms as tools with specific jobs. Understanding the job—and the boundary around it—is more useful than treating any cryptographic label as a guarantee.

Where HMAC is used

HMAC can be useful wherever two systems share a secret and need to check that data has not been altered. Examples include:

  • API request signing: A service may use an HMAC tag to authenticate a request and protect selected request details from undetected changes.
  • Message authentication: Systems can attach tags to messages so the receiving side can verify them using the shared key.
  • Keyed derivation: HMAC is also used as a component in established key-derivation constructions, where its role is different from simply authenticating a transmitted message.
  • Integrity checks within protocols: A protocol may rely on HMAC as one part of a larger design that defines what data is protected and how keys are handled.

These examples are not instructions to invent your own protocol. In security engineering, using a well-reviewed standard library and protocol is generally safer than assembling cryptographic parts from scratch.

What HMAC does not protect you from

An HMAC check can be strong and still leave important risks untouched. Keep these limits in view:

  • It does not encrypt. Someone who can see the message may still be able to read it. Use an appropriate encryption method when confidentiality is required.
  • It does not distinguish people who share a key. Any party with the key can create a valid tag. HMAC is not a digital signature and does not provide the same kind of public verification.
  • It does not stop replay by itself. An attacker might resend an old, valid message and tag. Protocols often address this with timestamps, sequence numbers, nonces, or other freshness checks.
  • It cannot rescue a stolen key. Anyone who obtains the secret can generate tags that appear valid. Key protection, rotation, and access control are essential.
  • It does not establish truth. A valid tag shows that the message passed an authentication check under the key—not that its claims are accurate or that its sender had good intentions.

For related reading, explore Understanding Data Integrity and Checksums and Hashes.

Key handling and verification details

The shared secret is the heart of HMAC’s authentication property. It should be generated and stored using appropriate security practices, shared only with the systems that need it, and replaced according to the organization’s key-management plan. Avoid placing secrets in public code repositories, logs, URLs, or other locations where they may be exposed.

Verification should follow the relevant protocol or trusted library’s guidance. In particular, implementations should use a comparison method designed for authentication tags rather than a naïve comparison that may reveal timing information. They should also reject invalid tags cleanly and avoid processing unauthenticated data as if it were trusted.

Algorithm choice matters, too. HMAC is a construction that works with specified hash functions; it is not itself a hash algorithm. For current context on common hash families and legacy choices, see SHA-2 and SHA-3 Explained. Follow current standards and expert guidance when selecting algorithms and parameters for a real system.

The takeaway

HMAC combines a cryptographic hash function with a shared secret key to create an authentication tag. When the recipient verifies that tag, a valid result supports the conclusion that the message has not changed and came from someone who knows the key. That makes HMAC a valuable tool for message integrity and authentication—but not for hiding data, identifying one person among key holders, or preventing every kind of attack.

The clearest way to remember it: a hash summarizes, HMAC authenticates with a shared secret, and encryption protects confidentiality. Knowing which problem you are solving is the first step toward using the right tool responsibly.

Theme