โš™๏ธ How HMAC Works: Hash + Secret Key

Understand the two-pass hashing process that creates authenticated messages

โ†
Previous
Introduction

โš™๏ธ How HMAC Works

HMAC is surprisingly elegant: it's just hashing with a twist! Let's break down the process step by step.

๐ŸŽฎ Interactive Step-by-Step Walkthrough

Watch how HMAC transforms your message and secret key into a signature. Click through each step!

๐Ÿ“ Step 1: Start with Message + Secret Key
Your Message:
Transfer 10 BTC to Alice
Your Secret Key:
my-secret-key-12345

๐Ÿ’ก Key Point: The secret key is shared between you and the server, but never transmitted in the request!

๐Ÿงช Try Your Own Inputs

Generated HMAC Signature:
000000005de73953

๐Ÿ’ก Try it: Change just one letter in the message. Notice how the entire signature changes completely!

๐Ÿ” The HMAC Formula

HMAC(K, M) =
H( (K โŠ• opad) || H( (K โŠ• ipad) || M ) )
K
= Secret Key
M
= Message
H
= Hash Function (SHA-256)
โŠ•
= XOR operation
||
= Concatenation
ipad
= Inner padding (0x36 repeated)
opad
= Outer padding (0x5C repeated)

๐Ÿ›ก๏ธ Why This Design Is Secure

๐Ÿ”’
Secret Key Never Transmitted

The key stays on your machine and the server. Only the signature travels over the network.

โšก
Avalanche Effect

Changing one bit in the message or key completely changes the signature. Attackers can't make small modifications.

๐Ÿšซ
Can't Reverse Engineer

Even if attackers see thousands of signatures, they can't figure out the secret key due to hash one-way property.

๐ŸŽฏ
Collision Resistant

Practically impossible to find two different messages with the same HMAC signature (for the same key).

โš ๏ธ What Could Go Wrong?

โŒ Weak Secret Key

Using short or predictable keys (like "123456") makes brute-force attacks possible.

โœ… Use long, random keys (at least 32 bytes/256 bits)
โŒ Key Leakage

If the secret key is exposed (committed to Git, logged, etc.), all security is lost.

โœ… Store keys in environment variables, never in code
โŒ Using MD5 or SHA-1

Older hash functions have known vulnerabilities that can compromise HMAC.

โœ… Always use SHA-256 or SHA-3