VillageSQL is a drop-in replacement for MySQL with extensions.
All examples in this guide work on VillageSQL. Install Now →
hmac() lets you do it in SQL.
The Problem: No Built-In HMAC
MySQL hasSHA2() and MD5() for hashing, but nothing that takes a key. HMAC (Hash-based Message Authentication Code) uses a shared secret to produce an authentication code — it’s fundamentally different from a plain hash because someone without the key can’t reproduce it.
The typical approach is to compute HMACs in the application before writing to the database:
With VillageSQL: hmac() in SQL
VillageSQL’svsql_crypto extension adds hmac(data, key, algorithm), which returns a VARBINARY authentication code computed entirely in MySQL.
Signing rows in a trigger
Use a trigger to sign every row at write time, regardless of which application path created it:Verifying webhook payloads
If your application stores incoming webhook payloads, you can verify signatures directly in SQL before processing:Supported algorithms
Use HMAC-SHA256 for new code. The HMAC construction doesn’t inherit the collision vulnerabilities of the underlying hash, so HMAC-SHA1 isn’t broken the way plain SHA-1 is — but SHA-1 is deprecated and regulators treat it as legacy. Don’t use it for new code.
HMAC vs. Plain Hash
If the data you’re protecting is public, a plain hash doesn’t prove anything. Use HMAC when you need to prove the data was produced by someone who knows the secret. For hashing without a key, see Hashing Data in MySQL.
Frequently Asked Questions
Is storing the HMAC key in the trigger safe?
No — it’s hardcoded in the trigger definition, visible to anyone withSHOW CREATE TRIGGER access. For production, retrieve the key from a secure configuration path or pass it as a session variable set by the application on connection. Treat the key as a credential.
Can I use HMAC to verify query results haven’t been altered in transit?
HMAC protects against unauthorized modifications, not eavesdropping. For in-transit protection, use TLS for your MySQL connection. HMAC is useful for detecting tampering after data reaches the database.How do I rotate the HMAC key?
Re-sign all existing rows with the new key before switching:Does hmac() return the same output for the same inputs?
Yes — HMAC is deterministic. The same data + same key + same algorithm always produce the same result. This is what makes verification work.Troubleshooting
See also
- Hashing Data in MySQL — keyless hashing for fingerprints and deduplication
- Sending Webhooks from Triggers — HMAC is the standard way to sign webhook payloads

