Field notes. I see these two mixed up in design reviews constantly, so here is the short, opinionated version.

A plain hash proves nothing about who made a message. Anyone can compute a SHA-256 of anything. If an attacker can change a message in transit, they can change the hash next to it too. So for “was this tampered with, and did it come from someone I trust?”, you need a secret in the picture. There are two ways to bring one in, and they make very different promises.

HMAC: one shared secret

HMAC mixes a secret key into a hash function in a specific, well-studied way. You and the other party both hold the same key. The sender computes the tag over the message and sends both. The receiver recomputes the tag with the same key and compares. If they match, two things are true: the message was not changed, and whoever made the tag knew the key.

This is how most payment gateway webhooks work. Your gateway signs each callback body with a secret you both share, and your server recomputes and compares before trusting a “payment successful” notification. Without that check, anyone who finds your webhook URL can post a fake success and walk away with goods. My opinion: skipping webhook verification is the single most common cheap-to-fix hole in small e-commerce backends.

Two implementation notes. First, compare the tags with a constant-time comparison function from your language’s crypto library, not a normal string equality check, which can leak how many leading characters matched through timing. Second, HMAC over the raw bytes you received. If you parse the JSON and re-serialise it, key order and spacing may change and verification will fail, or worse, be bypassed in a subtle way.

The weakness is built in. Both sides hold the key, so both sides could have produced any given tag. If the gateway and the merchant later disagree about whether a message was sent, HMAC cannot settle it. The merchant could have forged it, as far as an outsider can tell. This property is called lack of non-repudiation, and it is not a bug. It is what shared-secret designs are.

Digital signatures: a private key and a public key

In a digital signature scheme such as RSA-based signing or ECDSA, the signer holds a private key and publishes a public key. In practice the signer hashes the document and signs the hash with the private key. Anyone with the public key can verify. Nobody who lacks the private key can produce a valid signature, and the verifier needs no secret at all.

That changes what you can prove. If a bank signs your account statement PDF, you can hand the PDF to a third party, such as a court or a visa officer, and they can check it independently without ever contacting the bank or holding any shared secret. This is why India’s IT Act, 2000 gives legal recognition to digital signatures, and why eSign services built on Aadhaar authentication rely on signing, not shared secrets. Software updates work the same way: the vendor signs, millions of devices carry only the public key, and nobody has to distribute a secret to all of them.

The cost is speed and key handling. Signatures are slower than HMAC, and the signer’s private key becomes a crown jewel. If it leaks, an attacker can sign anything as you. That is why serious signing keys live in hardware security modules or managed key services, and why revocation exists.

Choosing between them

QuestionHMACDigital signature
KeysOne shared secretPrivate key signs, public key verifies
Who can create a valid tagAnyone holding the keyOnly the private key holder
Third-party verificationNo, verifier must hold the secretYes, public key is enough
SpeedVery fastSlower
Typical useWebhooks, API request signing, session tokensContracts, software releases, certificates, JWTs signed with asymmetric keys

My rule of thumb: if exactly two parties trust each other and nobody else ever needs convincing, use HMAC. If the proof must survive a dispute or be checked by many strangers, sign.

What neither one does

Neither hides the message. Both are about integrity and origin, not secrecy. If the content is sensitive you still need encryption, and the sound order is a matter of design, not taste; authenticated encryption modes combine both in one step so you do not hand-roll it.

Neither stops a replay either. A perfectly valid signed message can be captured and sent again next week. You need a timestamp, a unique ID or a nonce inside the signed content, and the receiver has to track what it has seen.

And neither tells you the key holder is honest. A signature from a stolen key verifies perfectly. The mathematics is fine; the trust comes from how keys are issued, stored and revoked.

One more opinion: never invent your own scheme, such as hashing the secret and message by simple concatenation. That construction has known weaknesses with some hash functions. Use the standard HMAC function your library already provides and move on to the next problem.

Leave a Reply

Trending