HMAC (Hash-based Message Authentication Code) is a message authentication mechanism formed by combining a specific hash function (such as MD5, SHA1, SHA256) with a cryptographic key.
Hash(message + key), HMAC employs a double-hash construction (inner and outer padding), effectively resisting "length extension attacks".This tool supports various mainstream hash functions as underlying implementations to meet the security requirements of different scenarios:
| Algorithm Name | Output Length | Security Level | Recommended Scenarios |
|---|---|---|---|
| HMAC-SHA256 | 256 bits (64 chars) | Very High | Modern API Authentication (e.g., AWS, Alibaba Cloud SDK) |
| HMAC-SHA1 | 160 bits (40 chars) | Medium | Legacy OAuth protocols, Git hash verification |
| HMAC-MD5 | 128 bits (32 chars) | Low | Scenarios with extremely high performance demands and non-critical security |
| HMAC-SHA512 | 512 bits (128 chars) | Highest | Financial-grade security and highly confidential data verification |
The operational logic of HMAC can be summarized in the following steps:
Q: What is the difference between HMAC and ordinary SHA256 encryption?
A: Ordinary SHA256 only requires the message content to generate a fingerprint; whereas HMAC must be provided with a key (Key). Even if the message is identical, different keys will generate completely different HMAC values. This is commonly used to prevent others from forging messages.
Q: Why does the HMAC value I generate not match the server's verification?
A: Please check the following three points:
Q: Is HMAC reversible? Can it be decrypted?
A: No, it is irreversible. HMAC belongs to the category of one-way hash functions. It can only be used for "verification," meaning the receiver recalculates it using the same algorithm and key and compares the results for consistency.
crypto, Python hmac library, and Java Mac class library.More HMAC Basics: HMAC Basics: Concept, Mathematical Structure, Security, and Application Scenarios