跳到主要内容
uencoder

HMAC

HMAC(Hash-based Message Authentication Code)是结合哈希函数与密钥的消息认证码,提供完整性与认证。

用途

在已有哈希函数上构造安全的 MAC,无需专用设计。

机制

HMAC(K, m) = H((K ⊕ opad) || H((K ⊕ ipad) || m)),其中 ipad/opad 为固定填充。密钥长于哈希块则先哈希。需配合安全哈希(SHA-256)使用;HMAC-MD5/SHA1 虽哈希已破但 HMAC 仍相对安全。

用例

  • API 请求签名(如 AWS SigV4)
  • JWT HS256 签名
  • TLS 记录 MAC
  • Cookie 防篡改

示例

  • 输入
    key + message
    输出
    HMAC-SHA256 tag

    32-byte authentication tag

常见错误

Using the bare hash H(key||msg) as a MAC (vulnerable to length-extension attacks)
Always use the HMAC construction

相关词条

相关工具

参考链接