What is Hash-based Message Authentication Code (HMAC)? – TechTarget Definition (2024)

What is Hash-based Message Authentication Code (HMAC)? – TechTarget Definition (1)

By

  • Rahul Awati

What is Hash-based Message Authentication Code (HMAC)?

Hash-based Message Authentication Code (HMAC) is a message encryption method that uses a cryptographic key in conjunction with a hash function. It provides the server and the client each with a private key that is known only to that specific server and client, providing a more secure means of encrypting data than a simple Message Authentication Code (MAC).

HMAC is a technique for cryptographic authentication. It uses both a cryptographic hash function and a shared secret key to encrypt information and protect it from unauthorized access. A hash function is an algorithm or mathematical function that converts a message that consists of a variable number of characters into a string with a fixed number of characters. The output value is known as the message digest, hash value or simply hash. The secret cryptographic key is what enables a user to make an encrypted message readable after it has been encrypted by an algorithm.

In an HMAC transaction, the client and server must agree on the secret key. This provides a way to decode messages, which must stay secret, to maintain the transaction's integrity. The parties must also choose and agree on the hash function for their messages.

HMAC can be used to check for data integrity and to authenticate the parties involved in a transaction. Many communication and transfer protocols use HMAC, including HTTPS, SFTP and FTPS. The cryptographic hash function in HMAC is typically SHA-1, SHA-256, MD5 or RIPEMD-128/160.

What is Hash-based Message Authentication Code (HMAC)? – TechTarget Definition (2)

How Hash-based Message Authentication Code works

HMAC provides a valid and reliable way for transacting parties to ensure that their messages have not been tampered by an unauthorized or malicious party, also referred to as a threat actor. The HMAC code or key consists of two parts:

  1. A shared set of cryptographic keys for the sender (client) and recipient (server). The sender and recipient use the same key to generate and verify the HMAC.
  2. A generic cryptographic hash function, like SHA-1 or RIPEMD-128/60.

The formula for HMAC is represented as the following:

HMAC = hashFunc(secret key + message)

In a messaging transaction between a client and a server involving HMAC, the client creates a unique HMAC or hash by hashing the request data with the private keys and sending it as part of a request. The server receives the request and regenerates its own unique HMAC. It then compares the two HMACs. If they are equal, the client is trusted and considered legitimate, and the request is executed. This process is often called a secret handshake.

Security in Hash-based Message Authentication Code

HMAC is more secure than MAC because the key and the message are hashed in separate steps:

HMAC(key, msg) = H(mod1(key) || H(mod2(key) || msg))

The client first hashes the data with a private key and sends it as a part of the request to the server. The server then makes its own HMAC. This ensures the process is not susceptible to extension attacks that can cause elements of the key to be leaked as successive MACs are created.

Also, once the process is complete, the sent message becomes both irreversible and resistant to hacking. Even if a malicious party tries to intercept the message, they won't be able to guess its length or decrypt it because they won't have the decryption key. In effect, the HMAC process renders the message contents unreadable and useless to the hacker.

Applications and benefits of HMAC

HMAC is considered a secure method to authenticate messages because it is difficult to forge if the potential forger doesn't know the secret shared key. The method is also resistant to dictionary attacks, where an attacker uses brute force to decode their way into a password-protected device by running through common words and phrases in a dictionary. That said, it's important to use a strong and unique secret key to maximize the security benefits of HMAC.

Since HMAC provides dual levels of protection, it is ideal for applications involving sensitive data, like personally identifiable information or credit card numbers. It provides more extensive security than traditional measures, making it suitable for use in regulated industries, like healthcare or finance.

HMAC encryption is also suitable for internet of things environments, high-performance systems like routers and verification of a user's email address. HMACs can also be used in security-critical applications where public key systems are either inadequate or prohibited. Possible applications include the following:

  • Authenticate form data sent to a client browser and resubmit.
  • Generate secure hashes for storing passwords.
  • Generate unique session management tokens.

Hash-based Message Authentication Code vs. digital signature

A digital signature is a way to validate the authenticity and integrity of a digital document and sender with the help of asymmetric public key cryptography. Like HMACs, digital signatures also employ a hash function and a shared key. However, HMACs use a symmetric key -- i.e., the same key is shared between the sender and recipient -- while a digital signature uses asymmetric keys, meaning the sender and recipient use two different keys.

Both HMACs and digital signatures ensure the integrity and authenticity of the message. Integrity means that the message has not been altered. If it is, the hash function gives a different value in return, so the recipient knows that someone has tampered with the message. Authenticity indicates that the recipient is confident that the message originates from the sender. Both methods ensure integrity and authenticity because the keys that encrypt the hash are unknown to a third party -- who may be a malicious adversary -- thus proving to the recipient that the message came from the expected sender.

In addition to ensuring integrity and authenticity, digital signatures are also used for nonrepudiation, which means that neither the sender nor recipient can deny having processed the information once it has been sent. HMACs are generally not used for nonrepudiation purposes.

See how MAC and HMAC use hash function encryption for authentication, and learn the differences between symmetric vs. asymmetric encryption.

This was last updated in May 2023

Continue Reading About Hash-based Message Authentication Code (HMAC)

  • Adopting threat hunting techniques, tactics and strategy
  • Passkey vs. password: What is the difference?
  • 5 fundamental strategies for REST API authentication
  • Where cloud cryptography fits in a security strategy
  • 3 types of PKI certificates and their use cases

Related Terms

cloud encryption
Cloud encryption is a service cloud storage providers offer whereby a customer's data is transformed using encryption algorithms ...Seecompletedefinition
cryptography
Cryptography is a method of protecting information and communications using codes, so that only those for whom the information is...Seecompletedefinition
sensitive information
Sensitive information is data that must be protected from unauthorized access to safeguard the privacy or security of an ...Seecompletedefinition

Dig Deeper on Data security and privacy

  • cryptographyBy: KathleenRichards
  • one-time passwordBy: KathleenRichards
  • message authentication code (MAC)By: RobertSheldon
  • How to use a public key and private key in digital signaturesBy: JoelDubin
What is Hash-based Message Authentication Code (HMAC)? – TechTarget Definition (2024)

FAQs

What is Hash-based Message Authentication Code (HMAC)? – TechTarget Definition? ›

HMAC is a cryptographic authentication technique that uses a secret key in conjunction with a hash function approved by the Federal Information Processing Standards (FIPS).

What is HMAC hash based message authentication code? ›

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 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.

What is the difference between hash and HMAC? ›

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 message authentication code? ›

Message Authentication Code (MAC), also referred to as a tag, is used to authenticate the origin and nature of a message. MACs use authentication cryptography to verify the legitimacy of data sent through a network or transferred from one person to another.

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.

How is hashing used for authentication? ›

If the hashes match, you grant the user access and if they do not match, you deny the user access. This method of authentication enables you to verify a user's identity without storing or transmitting their passwords in plain text, thus reducing the risk of password theft or leakage.

What is HMAC for dummies? ›

HMAC stands for Keyed-Hashing for Message Authentication. It's a message authentication code obtained by running a cryptographic hash function (like MD5, SHA1, and SHA256) over the data (to be authenticated) and a shared secret key. HMAC is specified in RFC 2104. HMACs are almost similar to digital signatures.

What is signing with HMAC authentication? ›

HMAC Signing is an access token method that adds another level of security by forcing the requesting client to also send along a signature that identifies the request temporally to ensure that the request is from the requesting user, using a secret key that is never broadcast over the wire.

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 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.

Is HMAC still secure? ›

One of the most commonly used hash functions in HMAC is SHA-256, which is considered secure and efficient.

What is hash-based message authentication code? ›

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.

Why am I being asked for an authentication code? ›

That's a form of two-factor authentication that adds a layer of security to your account — and keeps would-be scammers and hackers out. Your account password and a verification code work together, like the lock on your doorknob and a deadbolt lock.

What is the meaning of authentication code? ›

authentication code An appendage to a message that indicates to the recipient whether the message has been tampered with during transit. Authentication codes can be derived cryptographically as a function of the message and a secret key held by the sender and recipient. See also cryptography. A Dictionary of Computing.

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 OTP algorithm? ›

Time-based one-time password (TOTP) is a computer algorithm that generates a one-time password (OTP) using the current time as a source of uniqueness. As an extension of the HMAC-based one-time password algorithm (HOTP), it has been adopted as Internet Engineering Task Force (IETF) standard RFC 6238.

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.

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.

Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6146

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.