一、什么是HMAC | What is HMAC
HMAC(基于哈希的消息认证码)是一种基于密码学哈希函数与密钥结合而构造的消息认证机制,常用于验证消息的完整性与认证。
HMAC (Hash-based Message Authentication Code) is a mechanism that combines a cryptographic hash function with a secret key to verify the integrity and authenticity of a message.
它解决了单纯哈希函数无法使用密钥的问题,同时兼具效率和安全性。
It addresses the limitation that hash functions alone do not rely on a secret key, while maintaining both efficiency and security.
二、HMAC的设计动机 | Motivation for HMAC
哈希函数(如SHA-1)在软件中通常比传统加密算法(如DES)执行得更快
Cryptographic hash functions like SHA-1 generally execute faster than traditional encryption algorithms such as DES但SHA-1等哈希函数不能直接作为MAC使用,因为它们不依赖密钥
However, hash functions like SHA-1 cannot be directly used as MACs since they do not involve a secret key因此,研究者提出将密钥引入哈希算法中,最成功的方案就是HMAC
Hence, proposals emerged to incorporate a secret key into hashing. The most successful approach is HMAC
三、HMAC的设计目标 | Design Objectives of HMAC
使用现有哈希函数,无需修改算法本身
To use existing hash functions without modification支持在需要时替换为更快或更安全的哈希函数
To allow easy replacement of the underlying hash function if necessary保持哈希函数的原始性能,不显著降低效率
To preserve the original performance of the hash function简单地处理和使用密钥
To handle keys in a simple manner在理论上有良好的密码学分析基础
To provide strong theoretical security based on the embedded hash function
四、HMAC结构 | Structure of HMAC
HMAC通过对密钥进行两次填充(内填充ipad
和外填充opad
)并结合消息执行两次哈希,来实现安全的认证机制。
HMAC computes a MAC by performing two rounds of hashing using a padded key combined with the message.
结构如下:
内层哈希:
H((K ⊕ ipad) || M)
外层哈希:
H((K ⊕ opad) || 内层哈希结果)
最终得到的结果就是:
The final result is:
HMAC(K,M)=H((K⊕opad) ∣∣ H((K⊕ipad) ∣∣ M))
其中:
⊕:按位异或(XOR)
∣∣:连接操作
五、HMAC算法步骤 | HMAC Algorithm Steps
1. 密钥长度调整(Key Size Adjustments)
如果密钥 K 长于区块大小 b,先通过哈希函数压缩为 b 位
If the key is longer than block size b, hash it to shorten如果短于 b,则在右侧补零以对齐
If shorter, pad with zeros to reach b bits
2. 填充值常量(Padding Constants)
ipad:
0x36
,内层填充值opad:
0x5C
,外层填充值
这些常量与密钥异或后用于保护哈希的结构。
These constants are XORed with the key to pseudorandomly modify it.
3. 双重哈希(Double Hashing)
第一轮:计算 H(K⊕ipad ∣∣ M)
Inner hash over modified key and message第二轮:将第一轮结果作为输入,计算 H(K⊕opad ∣∣ InnerHash)
Outer hash adds another layer of security
六、简化示例练习 | Simplified Practice Example
设定条件:
哈希函数:统计输入中1的个数并模256
H(X)=sum of bits in Xmod 256
(这个的H(X)是密钥K扔进H(X)计算得到计算后的K(值和位数),这个H(X)公式由题目给出)
密钥 K=0b10101010(8位)
消息 M=0b11110000(8位)
(0b
是 二进制数(binary number) 的前缀)
区块大小 b=8
填充值:
ipad =
0b00110110
(0x36)opad =
0b01011100
(0x5C)
计算步骤:
K ⊕ ipad = 0b10101010 ⊕ 0b00110110 = 0b10011100
内部消息拼接:
10011100 || 11110000
哈希(统计1的个数):
4 (来自10011100) + 4 (来自11110000) = 8
(看条件使用哈希函数H(X))
内部哈希结果:8
然后继续外层哈希:
K ⊕ opad = 0b10101010 ⊕ 0b01011100 = 0b11110110
外部消息拼接:
11110110 || 00001000
(因为内部哈希结果是8)总1的个数是7(看条件使用哈希函数H(X))
最终HMAC为:7
Final HMAC output:
HMAC(K,M)=7
第二个例子:H(x)=((x>>4)⊕ x) and 0FF
(H代表的是十六进制)
七、总结 | Summary
HMAC是一种安全、灵活、效率高的认证机制,其主要特性包括:
HMAC provides a well-balanced tradeoff between security and performance, and is widely adopted in secure communications and data integrity verification.