

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: What Is a Message Integrity?
incomplete
2: Message Authentication Code (MAC)
incomplete
3: Message Authentication Code (MAC)
incomplete
4: Hash-Based Message Authentication Code
incomplete
5: Toy HMAC
incomplete
6: MACs and JWTs
incomplete
7: Asymmetric JWTs
incomplete
8: Asymmetric JWT Review
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Click to play video
An HMAC is a kind of MAC. All HMACs are MACs but not all MACs are HMACs. The main difference is that an HMAC uses two rounds of hashing instead of one (or none). Each round of hashing uses a child key that's derived from the secretKey.
Here's a naive implementation:
secretKey = 'thisIsASecretKey1234'
childKey1 = 'thisIsASe'
childKey2 = 'cretKey1234'
hash(childKey1 + hash(childKey2 + 'the message we want to send'))
This is a simplified version of the function given in RFC-2104.
With some MACs, depending on the hash function, it is possible to change the message (without knowing the key) and obtain another valid MAC. This is called a length extension attack. There are no known extension attacks against the current HMAC specification, so you should prefer HMACs over MACs.
In the naive example above, we created 2 child keys by splitting the original key. That's not the way a secure HMAC would be implemented, instead, we would derive child keys using a slightly more complex process. That said, the principle is the same: using a single key we can derive two separate child keys.
If you're curious about how that might work in production, you can check out the implementation here.