Class Pbkdf2
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Kdf
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Implements PBKDF2 (Password-Based Key Derivation Function 2) as defined in RFC 8018 §5.2 (formerly RFC 2898).
public static class Pbkdf2
- Inheritance
-
Pbkdf2
- Inherited Members
Examples
// Derive a 32-byte key from a password using HMAC-SHA-256 with 600,000 iterations
byte[] password = Encoding.UTF8.GetBytes("my-password");
byte[] salt = RandomNumberGenerator.GetBytes(16);
byte[] derivedKey = Pbkdf2.DeriveKey(
key => new HmacSha256(key),
password, salt, iterations: 600_000, outputLength: 32);
Remarks
PBKDF2 derives keying material from a password by applying a pseudorandom function (PRF) — typically HMAC — iteratively. The iteration count slows down brute-force attacks, making it suitable for password hashing and password-based encryption.
This implementation uses the CryptoHives IMac interface via the HmacFactory delegate, making it pluggable with any HMAC variant (SHA-256, SHA-384, SHA-512, SHA-1, etc.) and fully managed across all platforms.
Unlike the .NET Rfc2898DeriveBytes class, this implementation:
- Supports any PRF via HmacFactory (not limited to
HashAlgorithmName). - Provides span-based overloads for zero-copy key derivation.
- Works on all target frameworks including .NET Framework 4.6.2 and .NET Standard 2.0.
PBKDF2 is used in WPA/WPA2 key derivation, PKCS#12, S/MIME, password storage, and many enterprise authentication protocols.
Methods
DeriveKey(HmacFactory, byte[], byte[], int, int)
Derives keying material from a password using PBKDF2.
public static byte[] DeriveKey(HmacFactory hmacFactory, byte[] password, byte[] salt, int iterations, int outputLength)
Parameters
hmacFactoryHmacFactoryA factory that creates an IMac instance keyed with the given key.
passwordbyte[]The password (input keying material).
saltbyte[]The cryptographic salt.
iterationsintThe iteration count (c). Higher values increase resistance to brute-force attacks.
outputLengthintThe desired length of the derived keying material in bytes.
Returns
- byte[]
The derived keying material.
Exceptions
- ArgumentNullException
hmacFactory,password, orsaltis null.- ArgumentOutOfRangeException
outputLengthis less than 1 oriterationsis less than 1.
DeriveKey(HmacFactory, ReadOnlySpan<byte>, ReadOnlySpan<byte>, int, Span<byte>)
Derives keying material from a password using PBKDF2.
public static void DeriveKey(HmacFactory hmacFactory, ReadOnlySpan<byte> password, ReadOnlySpan<byte> salt, int iterations, Span<byte> output)
Parameters
hmacFactoryHmacFactoryA factory that creates an IMac instance keyed with the given key. The password is used as the HMAC key.
passwordReadOnlySpan<byte>The password (input keying material).
saltReadOnlySpan<byte>The cryptographic salt.
iterationsintThe iteration count (c). Higher values increase resistance to brute-force attacks. OWASP recommends at least 600,000 for HMAC-SHA-256.
outputSpan<byte>The destination buffer to receive the derived keying material.
Exceptions
- ArgumentNullException
hmacFactoryis null.- ArgumentException
outputis empty.- ArgumentOutOfRangeException
iterationsis less than 1.