Key Derivation functions in the Crypto DuckDB extension
Function category
Key Derivation
4 functionsPassword hashing and key derivation with Argon2, scrypt, PBKDF2 and HKDF. Use Argon2 or scrypt to store passwords, and HKDF to derive several keys from one high-entropy secret.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
variant
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
password
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
salt
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
iterations
|
Type
BIGINT
|
Mode Positional | Description |
Argument
memory_kib
|
Type
BIGINT
|
Mode Positional | Description |
Argument
parallelism
|
Type
BIGINT
|
Mode Positional | Description |
Argument
length
|
Type
BIGINT
|
Mode Positional | Description |
Description
Argon2 password hashing (RFC 9106). variant is 'argon2id' (recommended), 'argon2i' or 'argon2d' (case-insensitive); memory_kib is the memory cost in KiB (at least 32) and parallelism the number of lanes. The salt must be at least 8 bytes; output is length bytes (up to 1 MiB). Password and salt accept VARCHAR or BLOB. Working memory is reserved against DuckDB's memory_limit before it is allocated.
SELECT lower(to_hex(crypto_argon2('argon2id', 'password', 'somesalt', 2, 65536, 4, 32))) AS hash;
Output
| hash |
|---|
| 1a9677b0afe81fda7b548895e7a1bfeb8668ffc19a530e37e088a668fab1c02a |
SELECT lower(to_hex(crypto_argon2('argon2id', '', 'somesalt', 1, 64, 1, 16))) AS hash;
Output
| hash |
|---|
| 83b7ea15e3a517fe0455cbe091d0931c |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
algorithm
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
key
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
salt
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
info
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
length
|
Type
BIGINT
|
Mode Positional | Description |
Description
HKDF extract-and-expand (RFC 5869). Derives length bytes from high-entropy input key material, an optional salt, and a context info string; length is at most 255 × the digest length (8,160 bytes for sha2-256). Key, salt and info accept VARCHAR or BLOB. HKDF is fast, so it is for deriving keys from a secret, not for hashing passwords.
SELECT lower(to_hex(crypto_hkdf('sha2-256',
unhex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
unhex('000102030405060708090a0b0c'),
unhex('f0f1f2f3f4f5f6f7f8f9'),
42))) AS okm;
Output
| okm |
|---|
| 3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865 |
SELECT lower(to_hex(crypto_hkdf('sha2-256', unhex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), '', '', 42))) AS okm;
Output
| okm |
|---|
| 8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
algorithm
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
password
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
salt
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
iterations
|
Type
BIGINT
|
Mode Positional | Description |
Argument
length
|
Type
BIGINT
|
Mode Positional | Description |
Description
PBKDF2-HMAC key derivation (RFC 8018). Derives length bytes (1 to 1,048,576) from a password and salt with the given HMAC algorithm and iteration count (at least 1). Password and salt accept VARCHAR or BLOB. blake3 is not supported. Prefer crypto_argon2 for new password storage; use PBKDF2 where FIPS compliance or interoperability requires it.
SELECT lower(to_hex(crypto_pbkdf2('sha1', 'password', 'salt', 4096, 20))) AS dk;
Output
| dk |
|---|
| 4b007901b765489abead49d926f721d065a429c1 |
SELECT lower(to_hex(crypto_pbkdf2('sha2-256', 'passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, 40))) AS dk;
Output
| dk |
|---|
| 348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
password
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
salt
|
Type
BLOB | VARCHAR
2 concrete typesBLOBVARCHAR
|
Mode Positional | Description |
Argument
n
|
Type
BIGINT
|
Mode Positional | Description |
Argument
r
|
Type
BIGINT
|
Mode Positional | Description |
Argument
p
|
Type
BIGINT
|
Mode Positional | Description |
Argument
length
|
Type
BIGINT
|
Mode Positional | Description |
Description
scrypt password-based key derivation (RFC 7914). n is the CPU/memory cost and must be a power of 2, r the block size and p the parallelism; output is length bytes (up to 1 MiB). Password and salt accept VARCHAR or BLOB. Working memory (128 × n × r bytes) is reserved against DuckDB's memory_limit before it is allocated.
SELECT lower(to_hex(crypto_scrypt('password', 'NaCl', 1024, 8, 16, 64))) AS dk;
Output
| dk |
|---|
| fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640 |