Understanding MAC, HMAC, and CMAC: How to Secure Data with Python
This article explains the principles of Message Authentication Codes (MAC), details the operation and characteristics of HMAC and CMAC, compares common algorithms, and provides Python code examples for generating and verifying these codes to ensure data integrity and authenticity.
Introduction
Message Authentication Code (MAC) is a code generated with a secret key to verify both the integrity and authenticity of a message. It guarantees that the message has not been altered in transit and that the sender is trustworthy.
How MAC Works
The MAC algorithm takes a secret key and a message as inputs and produces a fixed‑length MAC value. The receiver recomputes the MAC with the same key and algorithm and compares the result with the received MAC; a match indicates that the message is complete and trustworthy.
Common MAC Algorithms
HMAC (Hash‑based Message Authentication Code)
Uses a hash function such as SHA‑256 together with a secret key.
Widely used in protocols like HTTPS and IPsec.
CMAC (Cipher‑based Message Authentication Code)
Uses a block cipher such as AES to generate the MAC.
Common in wireless communications and payment systems.
HMAC Detailed Overview
Structure
The HMAC computation consists of three steps:
Derive two key blocks, an outer block K_o and an inner block K_i, from the secret key and padding.
Compute the inner hash: H(K_i || message).
Compute the outer hash: H(K_o || H(K_i || message)). The final hash is the HMAC output.
Features
High security : The structure makes it infeasible for an attacker to recover the secret key from the MAC value.
Flexibility : Any secure hash function (SHA‑1, SHA‑256, etc.) can be used.
Standardized : Adopted in many standards such as TLS and IPsec.
HMAC Example Code (Python)
import hmac
import hashlib
# Define secret key and message
key = b'secret_key'
message = b'This is a secret message.'
# Generate HMAC value
h = hmac.new(key, message, hashlib.sha256)
hmac_value = h.hexdigest()
print(f"HMAC value: {hmac_value}")CMAC Detailed Overview
Structure
CMAC uses a block cipher (e.g., AES) and follows these steps:
Encrypt a zero block with the key to produce an intermediate key K1.
Shift K1 left; if the most‑significant bit is 1, XOR the result with a constant to obtain a second intermediate key K2.
Divide the message into blocks and encrypt each block; the final block is encrypted with K1 or K2 as appropriate.
Features
Hardware‑friendly : Because it is based on block‑cipher operations, CMAC is efficient for hardware implementation.
Standardized : Defined by NIST and used in many security protocols.
CMAC Example Code (Python, pycryptodome)
from Crypto.Hash import CMAC
from Crypto.Cipher import AES
# Define secret key and message
key = b'Sixteen byte key'
message = b'This is a secret message.'
# Generate CMAC value
c = CMAC.new(key, ciphermod=AES)
c.update(message)
cmac_value = c.hexdigest()
print(f"CMAC value: {cmac_value}")Conclusion
MAC algorithms are essential for guaranteeing data integrity and authenticity. HMAC and CMAC, with their high security and broad adoption, have become indispensable components of modern communication and data protection.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
