DEV Community

Discussion on: What is an HMAC?

Collapse
 
nathilia_pierce profile image
Nathilia Pierce

Looks like there is some misunderstandings, so I'll help.

Symmetric: MACs are not digital signatures. You can't verify the authenticity of someone's message without a secret key. Call it a MAC, or HMAC. This can allow for plausible deniability. These are easier to use, and understand like most symmetric cryptographic primitives. And generally more secure than asymmetric ones because of that.

You should use MAC/HMACs if you don't need someone else to verify your messages. <- This doesn't apply in all cases!

Asymmetric: Digital signatures imply anyone can verify the authenticity of a message, given a public key to a corresponding private key that was used to sign the message. Just like signing a document with your signature - everyone can verify that it was you who signed it. Nor can you deny you signed it.

You use this especially when signing software releases. Or signing emails with PGP.

Cryptographic keys are not passwords! Do not treat them like they are. Of course, make sure to keep them secret. Most of the time you use cryptographically secure pseudo-random numbers for the keys. Sometimes we use a Key Derivation Function to derive a cryptographic key from a low entroy value (passwords).


Length extension attacks are the reason we have HMACs. Primitives such as SHA1, SHA-256, MD5 are vulnerable, and that's just to name a few. Those hash functions make use of the Merkle–Damgård construction, which length extension attacks stemmed from. There are newer algorithms that aren't vulnerable, such as BLAKE2, or SHA-3.

# This is safe.
hash("sha3-256", $key . $message);

# THIS IS NOT!
# hash("sha256", $key . $message);
Enter fullscreen mode Exit fullscreen mode

I won't explain the details here, as the links are excellent sources to follow up on.