Skip to content

Hashing functions in the Crypto DuckDB extension

Function category

Hashing

4 functions

crypto_algorithms

Table function Hashing
Signature
crypto_algorithms() → TABLE
Returns

A table with the following columns:

Column name Type VARCHAR Description Algorithm name, as passed to crypto_hash and the other functions (case-insensitive).
Column family Type VARCHAR Description Algorithm family, e.g. sha2, sha3, shake, keccak, blake2.
Column implementation Type VARCHAR Description openssl, or blake3 for the vendored BLAKE3 library.
Column digest_length Type INTEGER Description Output length in bytes (the default length for XOFs); NULL when the algorithm is unavailable.
Column is_xof Type BOOLEAN Description Whether crypto_hash_xof can produce output of any length.
Column supports_hmac Type BOOLEAN Description Whether the algorithm can be used with crypto_hmac.
Column available Type BOOLEAN Description Whether the linked OpenSSL library provides the algorithm.
Description
1 The SHA-3 family and its extendable-output relatives
SELECT name, digest_length, is_xof, supports_hmac
FROM crypto_algorithms()
WHERE family IN ('sha3', 'shake')
ORDER BY name;

Output

name digest_length is_xof supports_hmac
sha3-224 28 false true
sha3-256 32 false true
sha3-384 48 false true
sha3-512 64 false true
shake128 32 true false
shake256 64 true false
2 Algorithms the linked OpenSSL doesn't provide
SELECT name, available FROM crypto_algorithms() WHERE NOT available;

Output

name available
md4 false

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(list_value('hello', 'world') AS VARCHAR[]))))
306a0d104017a29193be6c7464b1fd5ee65495353a7ccad7dd2928e5fb9731fd
4 Keccak-256 as used by Ethereum (differs from sha3-256)
SELECT lower(to_hex(crypto_hash('keccak256', 'abc'))) AS keccak256;

Output

keccak256
4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45
5 A UUID hashes as its 16 bytes, the same as hashing unhex() of its digits
SELECT crypto_hash('sha2-256', UUID '550e8400-e29b-41d4-a716-446655440000')
     = crypto_hash('sha2-256', unhex('550e8400e29b41d4a716446655440000')) AS same;

Output

same
true
6 DECIMAL hashes by unscaled value, so the declared width doesn't matter
SELECT crypto_hash('sha2-256', 1.00::DECIMAL(4,2)) = crypto_hash('sha2-256', 1.00::DECIMAL(38,2)) AS same;

Output

same
true
7 A fixed-size ARRAY hashes the same as the equivalent LIST
SELECT lower(to_hex(crypto_hash('sha2-256', [1, 2, 3]::INTEGER[3]))) AS digest;

Output

digest
4636993d3e1da4e9d6b8f87b79e8f7c6d018580d52661950eabc3845c5897a4d

crypto_hash_agg

Aggregate function Hashing
Signature
crypto_hash_agg(algorithm: VARCHAR, value: ANY) → BLOB
Arguments (Positional)
Argument algorithm Type VARCHAR Mode Positional Description
Argument value Type ANY 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(list_value(1, 2, 3, 4, 5) AS INTEGER[])))
true
3 Skip NULLs explicitly with FILTER
SELECT crypto_hash_agg('sha2-256', x ORDER BY x) FILTER (WHERE x IS NOT NULL)
     = crypto_hash('sha2-256', [1, 3]) AS matches
FROM (VALUES (1), (NULL), (3)) t(x);

Output

matches
true

crypto_hash_xof

Scalar function Hashing
Signature
crypto_hash_xof( algorithm: VARCHAR, value: ANY, length: BIGINT ) → BLOB
Arguments (Positional)
Argument algorithm Type VARCHAR Mode Positional Description
Argument value Type ANY Mode Positional Description
Argument length Type BIGINT Mode Positional Description
Description
1 100 bytes of SHAKE256
SELECT lower(to_hex(crypto_hash_xof('shake256', 'abc', 100))) AS digest;

Output

digest
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739d5a15bef186a5386c75744c0527e1faa9f8726e462a12a4feb06bd8801e751e41385141204f329979fd3047a13c5657724ada64d2470157b3cdc288620944d78dbcddbd9
2 The default length matches crypto_hash
SELECT crypto_hash_xof('blake3', 'abc', 32) = crypto_hash('blake3', 'abc') AS same;

Output

same
true