Skip to content

Hashing functions in the Crypto DuckDB extension

Function category

Hashing

2 functions

Compute cryptographic digests of values, lists, and entire datasets. Supports BLAKE3, BLAKE2b, the SHA-2 / SHA-3 families, plus legacy MD4 / MD5 / SHA-1 for compatibility. The aggregate variant produces an order-deterministic dataset checksum.

crypto_hash

Scalar function Hashing
Signature
crypto_hash(algorithm: VARCHAR, value: ANY) BLOB
Arguments (Positional)
Argument algorithm Type VARCHAR Mode Positional Description
Argument value Type ANY Mode Positional Description
Description
1 SHA-256 of a string, displayed as hex
SELECT lower(to_hex(crypto_hash('sha2-256', 'hello world')));

Output

lower(to_hex(crypto_hash('sha2-256', 'hello world')))
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
2 BLAKE3 of arbitrary integer
SELECT lower(to_hex(crypto_hash('blake3', 42::INTEGER)));

Output

lower(to_hex(crypto_hash('blake3', CAST(42 AS INTEGER))))
95dbc3244503309f26dee9436b39568cc80b0cbe17ef409e9273c4edb58653fd
3 Hash a list — each VARCHAR/BLOB element's length is hashed before its content
SELECT lower(to_hex(crypto_hash('sha2-256', ['hello', 'world']::VARCHAR[])));

Output

lower(to_hex(crypto_hash('sha2-256', CAST(main.list_value('hello', 'world') AS VARCHAR[]))))
306a0d104017a29193be6c7464b1fd5ee65495353a7ccad7dd2928e5fb9731fd

crypto_hash_agg

Aggregate function Hashing
Signature
crypto_hash_agg(algorithm: VARCHAR, value: BLOB | TINYINT | HUGEINT | …) BLOB
Arguments (Positional)
Argument algorithm Type VARCHAR Mode Positional Description
Argument value Type BLOB | TINYINT | HUGEINT | …
14 concrete types
BIGINTBLOBDOUBLEFLOATHUGEINTINTEGERSMALLINTTINYINTUBIGINTUHUGEINTUINTEGERUSMALLINTUTINYINTVARCHAR
Mode Positional Description
Description
1 Per-department checksum of employee IDs
SELECT department,
       lower(to_hex(crypto_hash_agg('sha2-256', employee_id ORDER BY employee_id))) AS dept_hash
FROM employees
GROUP BY department;
2 Aggregate hash matches list hash exactly when input is the same ordered set
SELECT crypto_hash_agg('sha2-256', value ORDER BY value)
     = crypto_hash('sha2-256', [1, 2, 3, 4, 5]::INTEGER[])
FROM (VALUES (1),(2),(3),(4),(5)) t(value);

Output

(crypto_hash_agg('sha2-256', "value" ORDER BY "value") = crypto_hash('sha2-256', CAST(main.list_value(1, 2, 3, 4, 5) AS INTEGER[])))
true