HMAC (Hash-Based Message Authentication Codes) Definition - Okta SG (2024)

Hash-based message authentication code (or HMAC) is a cryptographic technique that combines public keys, private keys, and a hash into a mix hackers can't unpack.

Use HMAC, and you'll tap into a method that can both encrypt data and check the integrity of information you get in return.

How HMAC Works

Two parties want to communicate, but they want to ensure that the contents of their connection remain private. They also distrust the internet, and they need a way to verify that the packets they receive haven't been tampered with. HMAC is a valid solution.

HMAC keys consist of two parts. These are:

  1. Cryptographic keys. An encryption algorithm alters data, and a recipient needs a specific code (or key) to make it readable once more. HMAC relies on two sets of keys. One is public, and one is private.
  2. Hash function. A hash algorithm alters or digests the message once more. HMAC uses generic cryptographic hash functions, such as SHA-1, MD5, or RIPEMD-128/60.

A pair using this system must agree on:

  • Public keys. They must have a way to contact one another. A public key is a bit like an address, and it allows a sender to craft a customised message.
  • Private keys. They must have a way to decode messages they get. A private key handles this task, and it's meant to stay secret and hidden.
  • Algorithm. They must pick one hash function that all of their messages will move through.

When complete, the message is considered irreversible, and it's also resistant to hacking. Someone who intercepts this message won't even be able to guess at its length. The work renders the message contents absolutely useless to anyone without a key or a code.

HMAC tester tools can help demonstrate how something like this works. Imagine you're dealing with these inputs:

  • Potential message: I would like to buy 100 units.
  • Secret key: 666
  • Algorithm: MD5

The resulting message reads: " fd9f18089206e67b163771a3883185ab."

A dense layer of mathematics underlies what seems like an easy translation process. When we attempt to display what HMAC looks like mathematically, we use diagrams like this.

Understanding the math is critical for developers. If you're asked to explain your work and the protections you offer, a diagram can often showcase things better than your words ever can.

But the average computer user may never need to understand the math. To them, knowing that their messages are protected may be all they require.

HMAC (Hash-Based Message Authentication Codes) Definition - Okta SG (1)

How to Implement HMAC

To use HMAC, either as an individual or a web developer, you'll need three important things. And you'll need an agreement about those items with your recipients, so you're all using the same tools at the same time.

These are the three items you'll need:

  1. A public key
  2. A private key
  3. A hashing tool

Only your server should know all three items for all of your users. And that data should be fiercely protected. Anyone who knows the public and private keys for your members can take over your server and/or send fraudulent data.

Every website and coding environment is different, but walking through an example might be helpful. Imagine that you'd like to use HMAC on traffic that comes to your website via dynamic ads from Google. You will:

  • Build your token in Ad Manager. You'll specify details about the visit and the time. You'll use Google's authentication key to create your "secret key."
  • Implement. You can put your new token within your authorisation request header, or you can pass it as a query string or form data parameter.

Google makes this process quick and easy. Developers can access a simple tutorial and copy code within minutes.

Notice that you're not asking your Google Ads visitors to memorise a code or do any decoding. The user's server understands the coding requirements within your website, and all of the token setting and translation is invisible to the user.

Even so, you should test this environment often before you deploy it. If you encounter a coding error, you could block people from accessing your site, as it will seem as though they're fraudulent actors. It pays to test any system like this on multiple devices before you set it loose on the wider world.

When Should You Use HMAC?

Nearly every company has sensitive information. If you take in payments of any sort, for example, you likely have credit card data at your fingertips. And if you have employees, you have Social Security numbers that could be stolen.

But some companies have even deeper issues. If you're in a heavily regulated environment, such as health care, or you deal with trade secrets, such as munitions, it pays to move past traditional security measures.

HMAC, with its dual levels of protection, could be ideal for companies that need to do a little more to prove that they're protecting their assets as carefully as possible.

Help From Okta

At Okta, we believe in customised security solutions to help our clients thrive. Learn how Okta uses HMAC signature algorithms to keep your organisation secure.

References

HMAC: Keyed-Hashing for Message Authentication. (February 1997). Network Working Group.

HMAC and Key Derivation. Practical Cryptography for Developers.

HMAC Generator/Tester Tool. FreeFormatter.

How API Request Signing Works (And How to Implement HMAC in NodeJS). (2016). Andrew Hoang.

Implement HMAC Authentication (Beta). Google Ad Manager Help.

HMAC (Hash-Based Message Authentication Codes) Definition - Okta SG (2024)

FAQs

HMAC (Hash-Based Message Authentication Codes) Definition - Okta SG? ›

In cryptography, an HMAC (sometimes expanded as either keyed-hash message authentication code or hash-based message authentication code) is a specific type of message authentication code (MAC) involving a cryptographic hash function and a secret cryptographic key.

What is HMAC hash-based message authentication codes? ›

Hash-based message authentication code (or HMAC) is a cryptographic authentication technique that uses a hash function and a secret key. With HMAC, you can achieve authentication and verify that data is correct and authentic with shared secrets, as opposed to approaches that use signatures and asymmetric cryptography.

What is the difference between a message authentication code and a hash code? ›

A message authentication code (MAC) is similar to a cryptographic hash, except that it is based on a secret key. When secret key information is included with the data that is processed by a cryptographic hash function, the resulting hash is known as an HMAC.

What is the primary difference between a hash and hashed message authentication code HMAC )? ›

A hash lets you verify only the authenticity of the data (i,. e., that the data you received is what was originally sent). An HMAC lets you verify both the authenticity and the originator of the data. A hash doesn't use a key.

What is the HMAC authentication key? ›

An HMAC key is a type of credential associated with an account, typically a service account. You use an HMAC key to create signatures using the HMAC-SHA256 signing algorithm. The signatures you create are then included in requests to the Cloud Storage XML API.

How does HMAC authentication work? ›

When two parties exchange messages through those secure file transfer protocols, those messages will be accompanied by HMACs instead of plain hashes. An HMAC employs both a hash function and a shared secret key. A shared secret key provides exchanging parties a way to establish the authenticity of the message.

What are the benefits of HMAC authentication? ›

Ultimately, HMAC provides a great layer of security for companies that have sensitive data that needs protecting. It's an important measure to protect data integrity from attackers and offers a clear indication if data has been compromised. See how Kinde compares to other authentication providers.

What is the purpose of the hash code? ›

Hashing means using some function or algorithm to map object data to some representative integer value. This so-called hash code (or simply hash) can then be used as a way to narrow down our search when looking for the item in the map.

What is difference between a message authentication code and a one way hash function? ›

Cryptographic has function is one-way function, which is practically infeasible to invert. The most popular hashing algorithm is MD5 and SHA. Message authentication is a mechanism or service used to verify the integrity of a message. Message authentication guarantees that the sender of the message is authentic.

What is a message authentication code for dummies? ›

A message authentication code uses a single key with the message and hashes it to validate the message's sender identity and ensure data integrity.

What are the disadvantages of HMAC? ›

One of the main drawbacks of HMAC is that it requires both parties to share a secret key in advance, which can be challenging in some scenarios, such as public key infrastructures or distributed systems.

Why is HMAC better than hash? ›

HMACs are substantially less affected by collisions than their underlying hashing algorithms alone. In particular, Mihir Bellare proved that HMAC is a pseudo-random function (PRF) under the sole assumption that the compression function is a PRF.

What does HMAC stand for? ›

Hash-Based Message Authentication Code (HMAC)

What is the best practice of HMAC authentication? ›

3 Best practices for HMAC

To use HMAC effectively and securely, it's recommended to use a strong hash function, such as SHA-256, SHA-384, or SHA-512. A secure key should be generated randomly, stored securely, and rotated periodically. The key should be long enough to prevent brute-force attacks.

What is the difference between HMAC and JWT? ›

A JWT (when using HMAC as the signing scheme) is basically just an HMAC message where the message data is a JSON object. The interesting thing about the JWT system is that the sender and the receiver of the JWT are typically the same entity, that is, the webserver.

Why does HMAC use two keys? ›

From memory: there was some fear that identical keys would produce a correlation that would weaken the algorithm. Then somebody proved that correlated keys can be safe, and regular HMAC was then specified to use a bitmask to derive a second key from the authentication key.

What is HMAC SHA-256 authentication code? ›

HMACSHA256 is a type of keyed hash algorithm that is constructed from the SHA-256 hash function and used as a Hash-based Message Authentication Code (HMAC).

What is hash in authentication? ›

A hash function is a key component of your authentication system, as it determines how secure and efficient your hashing is. You should choose a hash function that is fast, consistent, and resistant to attacks, such as brute force, rainbow tables, and length extension.

Is HMAC authentication or encryption? ›

HMAC does not encrypt the message. Instead, the message (encrypted or not) must be sent alongside the HMAC hash. Parties with the secret key will hash the message again themselves, and if it is authentic, the received and computed hashes will match.

How is the hash message authentication code HMAC algorithm used in an ipsec VPN? ›

Here is how an HMAC works, in its simplest form. First, both the server and the client agree to use the same cryptographic hash function (SHA-384) and establish a shared secret key. Then, the sender combines that shared secret key with the data being sent and creates a hash out the combination of the two.

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6339

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.