Skip to content

HMAC functions in the Crypto DuckDB extension

Function category

HMAC

2 functions

crypto_equals

Scalar function HMAC
Signature
crypto_equals(a: BLOB | VARCHAR, b: BLOB | VARCHAR) → BOOLEAN
Arguments (Positional)
Argument a Type BLOB | VARCHAR
2 concrete types
BLOBVARCHAR
Mode Positional Description
Argument b Type BLOB | VARCHAR
2 concrete types
BLOBVARCHAR
Mode Positional Description
Description
1 Verify an HMAC
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
2 Compares bytes, so VARCHAR and BLOB can be mixed
SELECT crypto_equals('A', unhex('41')) AS same;

Output

same
true

crypto_hmac

Scalar function HMAC
Signature
crypto_hmac( algorithm: VARCHAR, key: VARCHAR | BLOB, message: VARCHAR | BLOB ) → BLOB
Arguments (Positional)
Argument algorithm Type VARCHAR Mode Positional Description
Argument key Type VARCHAR | BLOB
2 concrete types
BLOBVARCHAR
Mode Positional Description
Argument message Type VARCHAR | BLOB
2 concrete types
BLOBVARCHAR
Mode Positional Description
Description
1 HMAC-SHA256 over a message with a secret key
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
2 RFC 4231 test case 1, with a BLOB key
SELECT lower(to_hex(crypto_hmac('sha2-256', unhex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), 'Hi There'))) AS mac;

Output

mac
b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7