crypt_pbkdf2() stretches a password into a key with PBKDF2 (RFC 8018,
section 5.2), using HMAC with the chosen digest as the pseudorandom
function.
Arguments
- password
The password, as a raw vector. A string is not accepted, because its bytes depend on an encoding: convert deliberately, for example with
charToRaw(enc2utf8(x)). It may be empty.- salt
The salt, as a raw vector. Use at least 16 random bytes from
crypt_rand_bytes(), a fresh salt for each password. It may be empty, which only interoperability with an existing format justifies.- iterations
The iteration count, a whole number from 1 to 4294967295. It has no default, because the right value depends on the digest, the hardware and the time you can afford. OWASP recommended 600,000 for
"sha256"in 2023.- length
The number of bytes to derive, a whole number of at least 1. 32 gives an AES-256 key.
- algorithm
The digest for the HMAC:
"sha256"(the default),"sha384","sha512"or"sha1"."sha1"is for formats that specify it, never for anything new.
Details
PBKDF2 derives a key. It is not an encryption format and not a password storage format. The salt, the iteration count and the digest are yours to choose, store beside what the key protects, and supply again to derive the same key.
Cost
The time is proportional to iterations times the number of digest-sized
blocks in length, and the call cannot be interrupted. Bound
iterations before passing a value that came from untrusted input, such
as a file header.
Errors
An invalid argument is zucrypt_invalid_argument, and an unknown digest
is zucrypt_unsupported_algorithm.
See also
crypt_aes_gcm_encrypt(), which takes the key this derives.
Examples
salt <- crypt_rand_bytes(16)
key <- crypt_pbkdf2(charToRaw("correct horse"), salt, iterations = 10000)
key
#> [1] 91 4a bd f8 66 ef 3e c6 3b 6d 2b b5 7e be 08 d3 b3 77 40 5f 57 b3 a6 18 f1
#> [26] a9 6c f6 4c e9 aa 06
# RFC 6070, test case 2.
crypt_hex(crypt_pbkdf2(charToRaw("password"), charToRaw("salt"),
iterations = 2, length = 20, algorithm = "sha1"))
#> [1] "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"