pbkdf2#
Password-Based Key Derivation Function 2 (RFC 8018) for OCaml.
Overview#
pbkdf2 provides a pure OCaml implementation of PBKDF2-HMAC-SHA256 as specified in RFC 8018. PBKDF2 derives cryptographic keys from passwords using a pseudorandom function (HMAC-SHA256), applying it iteratively to resist brute-force attacks.
Features#
- RFC 8018 compliant: Implements PBKDF2-HMAC-SHA256
- Configurable iterations: Control computational cost for security
- Pure OCaml: No C dependencies, works everywhere OCaml runs
- Fuzz tested: Includes Crowbar-based property tests
Installation#
opam install pbkdf2
Usage#
(* Derive a 32-byte key from a password *)
let key = Pbkdf2.derive
~password:"user_password"
~salt:random_salt
~iterations:100000
~length:32
API#
Pbkdf2.derive ~password ~salt ~iterations ~length- Derive a key using PBKDF2-HMAC-SHA256Pbkdf2.hash_length- Output length of the underlying hash (32 bytes for SHA-256)
Security Recommendations#
- Use at least 100,000 iterations for password hashing (OWASP recommendation)
- Generate random salts of at least 16 bytes
- Use unique salts per password
Related Work#
- kdf - Key derivation functions including HKDF, PBKDF, and SCRYPT. Uses mirage-crypto for PBKDF2-SHA256.
- pbkdf - PBKDF1/PBKDF2 implementation using nocrypto (archived, now part of kdf).
This library provides a minimal, standalone PBKDF2-HMAC-SHA256 implementation using digestif.
Licence#
MIT License. See LICENSE.md for details.