HMAC Generator
使用SHA-256、SHA-384或SHA-512生成HMAC签名
消息
密钥
HMAC签名
HMAC签名将显示在这里…
什么是HMAC?
HMAC(基于哈希的消息认证码)是RFC 2104中定义的一种密码学结构,它将哈希函数与密钥结合,生成固定长度的认证标签。与任何人都能计算的普通哈希不同,HMAC只能由共享密钥的双方生成和验证。HMAC是同时验证消息完整性和真实性的标准机制——确认数据未被篡改,且来自可信发送方。
HMAC算法可与任何迭代哈希函数配合使用:SHA-256、SHA-384、SHA-512,乃至SHA-1或MD5等旧版函数。所得结构以其底层哈希命名——HMAC-SHA256、HMAC-SHA384或HMAC-SHA512。由于HMAC的安全性证明依赖于哈希函数具备抗碰撞性和伪随机特性,SHA-2系列算法是新系统的推荐选择。HMAC-SHA256是部署最广泛的变体,用于AWS Signature V4、Stripe webhook、GitHub webhook密钥、Slack请求签名及JSON Web Token(HS256)。
HMAC的设计提供了普通哈希所缺乏的关键特性:对长度扩展攻击的抵抗能力。仅使用SHA-256时,知道H(message)的攻击者无需知道原始消息即可计算H(message || attacker_data)。HMAC的双重哈希结构(使用不同填充密钥的内层哈希和外层哈希)完全消除了这种攻击。这正是API签名方案使用HMAC而非简单地将密钥附加到消息后再哈希的原因。
为什么使用在线HMAC生成器?
计算HMAC签名通常需要编写代码或使用命令行工具。这个基于浏览器的HMAC生成器让你无需安装软件或切换到终端,即可立即生成HMAC-SHA256、HMAC-SHA384和HMAC-SHA512签名。
HMAC生成器使用场景
HMAC vs 普通哈希 vs 加密
HMAC、普通哈希和加密各有不同用途。HMAC提供消息认证——证明消息由持有密钥的人创建且未被修改。普通哈希提供完整性但不提供认证。加密提供机密性。下表阐明了三者的区别。
| 属性 | HMAC | Plain Hash | Encryption |
|---|---|---|---|
| Purpose | Message authentication + integrity | Data integrity only (no key) | Confidentiality + integrity |
| Requires secret key | Yes | No | Yes |
| Verifiable by | Parties who share the secret | Anyone | Recipient with key |
| Reversible | No — digest only | No — digest only | Yes — decryption recovers data |
| Output size | Depends on hash (e.g. 256 bits) | Depends on hash | Variable (ciphertext) |
| Standard | RFC 2104 | FIPS 180-4 | NIST SP 800-38A (AES) |
| Use case example | Webhook signature verification | File checksum verification | Encrypting data at rest |
HMAC算法比较
HMAC可使用任何哈希函数,但底层算法的选择决定了输出大小、安全级别和浏览器兼容性。HMAC-SHA256是新系统最常见的选择。下表比较了你可能遇到的各种变体。
| 算法 | 摘要大小 | 十六进制长度 | Web Crypto API | 适用场景 |
|---|---|---|---|---|
| HMAC-SHA256 | 256 bits | 64 hex chars | Yes | API signing, webhooks, JWT (HS256) |
| HMAC-SHA384 | 384 bits | 96 hex chars | Yes | TLS 1.3 PRF, CNSA compliance |
| HMAC-SHA512 | 512 bits | 128 hex chars | Yes | High-security signatures, HKDF |
| HMAC-SHA1 | 160 bits | 40 hex chars | Yes | Legacy OAuth 1.0, TOTP (RFC 6238) |
| HMAC-MD5 | 128 bits | 32 hex chars | No | Legacy only — not recommended |
HMAC内部工作原理
HMAC使用两种不同的密钥派生填充,对底层哈希函数应用两次。该结构定义于RFC 2104,并在标准密码学假设下被证明是PRF(伪随机函数)。密钥首先被填充或哈希,以匹配哈希函数的块大小(SHA-256为64字节,SHA-512为128字节)。
where K' = key padded to block size, ipad = 0x36, opad = 0x5C
算法将填充后的密钥与内层填充常量(ipad,0x36重复填充)进行XOR,与消息拼接后求哈希。然后将填充后的密钥与外层填充常量(opad,0x5C重复填充)进行XOR,与内层哈希输出拼接后再次求哈希。这种双重哈希结构正是防止长度扩展攻击、并确保HMAC输出在不知道密钥的情况下无法被计算的原因。
HMAC代码示例
HMAC在所有主流语言和运行时中均有原生支持。Web Crypto API无需任何库即可在浏览器中提供HMAC-SHA256、HMAC-SHA384和HMAC-SHA512。以下示例展示了包括webhook验证和常量时间比较在内的实际使用模式。
async function hmacSHA256(message, secret) {
const enc = new TextEncoder()
const key = await crypto.subtle.importKey(
'raw', enc.encode(secret),
{ name: 'HMAC', hash: 'SHA-256' },
false, ['sign']
)
const sig = await crypto.subtle.sign('HMAC', key, enc.encode(message))
return Array.from(new Uint8Array(sig))
.map(b => b.toString(16).padStart(2, '0')).join('')
}
await hmacSHA256('hello world', 'my-secret-key')
// → "90eb182d8396f16d4341d582047f45c0a97d73388c5377d9ced478a2212295ad"
// Node.js (built-in crypto module)
const crypto = require('crypto')
crypto.createHmac('sha256', 'my-secret-key')
.update('hello world').digest('hex')
// → "90eb182d8396f16d4341d582047f45c0a97d73388c5377d9ced478a2212295ad"import hmac
import hashlib
# HMAC-SHA256
sig = hmac.new(
b'my-secret-key',
b'hello world',
hashlib.sha256
).hexdigest()
print(sig)
# → "90eb182d8396f16d4341d582047f45c0a97d73388c5377d9ced478a2212295ad"
# Verify a webhook signature (constant-time comparison)
expected = "90eb182d8396f16d4341d582047f45c0a97d73388c5377d9ced478a2212295ad"
received = hmac.new(b'my-secret-key', b'hello world', hashlib.sha256).hexdigest()
if hmac.compare_digest(expected, received):
print("Signature valid")
# HMAC-SHA512
hmac.new(b'key', b'data', hashlib.sha512).hexdigest()package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
)
func main() {
mac := hmac.New(sha256.New, []byte("my-secret-key"))
mac.Write([]byte("hello world"))
sig := mac.Sum(nil)
fmt.Printf("%x\n", sig)
// → 90eb182d8396f16d4341d582047f45c0a97d73388c5377d9ced478a2212295ad
// Verify: use hmac.Equal for constant-time comparison
expected := mac.Sum(nil)
fmt.Println(hmac.Equal(sig, expected)) // true
}# HMAC-SHA256 echo -n "hello world" | openssl dgst -sha256 -hmac "my-secret-key" # → SHA2-256(stdin)= 90eb182d8396f16d4341d582047f45c0a97d73388c5377d9ced478a2212295ad # HMAC-SHA512 echo -n "hello world" | openssl dgst -sha512 -hmac "my-secret-key" # Verify a file signature openssl dgst -sha256 -hmac "my-secret-key" release.tar.gz # HMAC with hex key (e.g. from a webhook secret) echo -n "payload" | openssl dgst -sha256 -hmac "$(echo -n '736563726574' | xxd -r -p)"