HMAC functions in the Crypto DuckDB extension
Function category
HMAC
2 functionsKeyed message authentication codes and constant-time comparison. Use for signing API requests, verifying message integrity, or building tamper-evident audit trails — anywhere a hash needs a shared secret.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
a
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
b
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Description
Constant-time comparison of two VARCHAR or BLOB values, using OpenSSL's CRYPTO_memcmp. Returns true when they hold the same bytes. Use it instead of = when verifying an HMAC or other secret, so timing doesn't reveal how many leading bytes matched. NULL in either argument returns NULL.
SELECT crypto_equals(crypto_hmac('sha2-256', 'k', 'm'), crypto_hmac('sha2-256', 'k', 'm')) AS valid,
crypto_equals(crypto_hmac('sha2-256', 'k', 'm'), crypto_hmac('sha2-256', 'k', 'n')) AS tampered;
Output
| valid | tampered |
|---|---|
| true | false |
SELECT crypto_equals('A', unhex('41')) AS same;
Output
| same |
|---|
| true |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
algorithm
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
key
|
Type
VARCHAR | BLOB
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
message
|
Type
VARCHAR | BLOB
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Description
Hash-based Message Authentication Code (HMAC). Combines a secret key with a message under a specified hash algorithm — used for message integrity and API request signing. The key and message may each be VARCHAR or BLOB, so keys from crypto_random_bytes or unhex() work directly. blake3 uses its keyed mode and needs a 32-byte key; shake128 and shake256 are not supported. Verify a MAC with crypto_equals.
SELECT lower(to_hex(crypto_hmac('sha2-256', 'my-secret-key', 'important message')));
Output
| lower(to_hex(crypto_hmac('sha2-256', 'my-secret-key', 'important message'))) |
|---|
| 97f324adef061b4ad0abeb6be543913d7db6ba8e6e7f33cd3c4395d619b56df4 |
SELECT lower(to_hex(crypto_hmac('sha2-256', unhex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), 'Hi There'))) AS mac;
Output
| mac |
|---|
| b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7 |