Class CryptoHkdf
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Provides HKDF key derivation (RFC 5869) using SHA-256 or SHA-512.
public static class CryptoHkdf
- Inheritance
-
CryptoHkdf
- Inherited Members
Fields
MinOkmLen
Minimum length of output key material (OKM) in bytes (4).
public const int MinOkmLen = 4
Field Value
Sha256MaxOkmLen
Maximum length of output key material (OKM) for SHA256 in bytes (8160 = 32 * 255).
public const int Sha256MaxOkmLen = 8160
Field Value
Sha256PrkLen
Length of the pseudorandom key (PRK) for SHA256 in bytes (32).
public const int Sha256PrkLen = 32
Field Value
Sha512MaxOkmLen
Maximum length of output key material (OKM) for SHA512 in bytes (8160 = 64 * 255).
public const int Sha512MaxOkmLen = 16320
Field Value
Sha512PrkLen
Length of the pseudorandom key (PRK) for SHA256 in bytes (32).
public const int Sha512PrkLen = 64
Field Value
Methods
DeriveKey(HashAlgorithmName, SecureMemory<byte>, SecureMemory<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Derives key material from input key material in one step using HKDF (RFC 5869).
public static void DeriveKey(HashAlgorithmName hashAlgorithmName, SecureMemory<byte> ikm, SecureMemory<byte> okm, ReadOnlySpan<byte> salt, ReadOnlySpan<byte> info)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmSecureMemory<byte>Input keying material.
okmSecureMemory<byte>Output buffer to receive the derived keying material (16–64 bytes).
saltReadOnlySpan<byte>Optional salt value (can be empty).
infoReadOnlySpan<byte>Optional context and application-specific information.
Exceptions
- ArgumentException
Thrown if
okmor internal buffers have invalid lengths.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
DeriveKey(HashAlgorithmName, Stream, SecureMemory<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Derives key material from input key material in one step using HKDF (RFC 5869) from a stream.
public static void DeriveKey(HashAlgorithmName hashAlgorithmName, Stream ikm, SecureMemory<byte> okm, ReadOnlySpan<byte> salt, ReadOnlySpan<byte> info)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material.
okmSecureMemory<byte>Buffer to receive the output keying material.
saltReadOnlySpan<byte>Optional salt value.
infoReadOnlySpan<byte>Optional application-specific information.
Exceptions
- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
DeriveKey(HashAlgorithmName, Stream, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Derives key material from input key material in one step using HKDF (RFC 5869) from a stream.
public static void DeriveKey(HashAlgorithmName hashAlgorithmName, Stream ikm, Span<byte> okm, ReadOnlySpan<byte> salt, ReadOnlySpan<byte> info)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material.
okmSpan<byte>Buffer to receive the output keying material.
saltReadOnlySpan<byte>Optional salt value.
infoReadOnlySpan<byte>Optional application-specific information.
Exceptions
- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
DeriveKey(HashAlgorithmName, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Derives key material from input key material in one step using HKDF (RFC 5869).
public static void DeriveKey(HashAlgorithmName hashAlgorithmName, ReadOnlySpan<byte> ikm, Span<byte> okm, ReadOnlySpan<byte> salt, ReadOnlySpan<byte> info)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmReadOnlySpan<byte>Input keying material.
okmSpan<byte>Output buffer to receive the derived keying material (16–64 bytes).
saltReadOnlySpan<byte>Optional salt value (can be empty).
infoReadOnlySpan<byte>Optional context and application-specific information.
Exceptions
- ArgumentException
Thrown if
okmor internal buffers have invalid lengths.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
DeriveKeyAsync(HashAlgorithmName, Stream, SecureMemory<byte>, ReadOnlyMemory<byte>, ReadOnlyMemory<byte>, CancellationToken)
Asynchronously derives key material from input key material in one step using HKDF (RFC 5869) from a stream.
public static Task DeriveKeyAsync(HashAlgorithmName hashAlgorithmName, Stream ikm, SecureMemory<byte> okm, ReadOnlyMemory<byte> salt, ReadOnlyMemory<byte> info, CancellationToken cancellationToken = default)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material.
okmSecureMemory<byte>Buffer to receive the output keying material.
saltReadOnlyMemory<byte>Optional salt value.
infoReadOnlyMemory<byte>Optional application-specific information.
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
DeriveKeyAsync(HashAlgorithmName, Stream, Memory<byte>, ReadOnlyMemory<byte>, ReadOnlyMemory<byte>, CancellationToken)
Asynchronously derives key material from input key material in one step using HKDF (RFC 5869) from a stream.
public static Task DeriveKeyAsync(HashAlgorithmName hashAlgorithmName, Stream ikm, Memory<byte> okm, ReadOnlyMemory<byte> salt, ReadOnlyMemory<byte> info, CancellationToken cancellationToken = default)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material.
okmMemory<byte>Buffer to receive the output keying material.
saltReadOnlyMemory<byte>Optional salt value.
infoReadOnlyMemory<byte>Optional application-specific information.
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
Expand(HashAlgorithmName, SecureMemory<byte>, SecureMemory<byte>, ReadOnlySpan<byte>)
Performs the expand step of HKDF (RFC 5869), using the specified hash algorithm.
public static void Expand(HashAlgorithmName hashAlgorithmName, SecureMemory<byte> prk, SecureMemory<byte> okm, ReadOnlySpan<byte> info)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
prkSecureMemory<byte>Pseudorandom key obtained from the extract step (32 or 64 bytes).
okmSecureMemory<byte>Output buffer to receive the derived keying material (4–8160 or 16320 bytes).
infoReadOnlySpan<byte>Optional context and application-specific information.
Exceptions
- ArgumentOutOfRangeException
Thrown if
okmis not in valid range.- ArgumentException
Thrown if
prkis not valid size for the selected hash.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
Expand(HashAlgorithmName, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>)
Performs the expand step of HKDF (RFC 5869), using the specified hash algorithm.
public static void Expand(HashAlgorithmName hashAlgorithmName, ReadOnlySpan<byte> prk, Span<byte> okm, ReadOnlySpan<byte> info)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
prkReadOnlySpan<byte>Pseudorandom key obtained from the extract step (32 or 64 bytes).
okmSpan<byte>Output buffer to receive the derived keying material (4–8160 or 16320 bytes).
infoReadOnlySpan<byte>Optional context and application-specific information.
Exceptions
- ArgumentOutOfRangeException
Thrown if
okmis not in valid range.- ArgumentException
Thrown if
prkis not valid size for the selected hash.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
Extract(HashAlgorithmName, SecureMemory<byte>, ReadOnlySpan<byte>, SecureMemory<byte>)
Performs the extract step of HKDF (RFC 5869), using the specified hash algorithm.
public static void Extract(HashAlgorithmName hashAlgorithmName, SecureMemory<byte> ikm, ReadOnlySpan<byte> salt, SecureMemory<byte> prk)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmSecureMemory<byte>Input keying material.
saltReadOnlySpan<byte>Optional salt value (can be empty).
prkSecureMemory<byte>Buffer to receive the pseudorandom key (32 bytes for SHA256 and 64 bytes for SHA512).
Exceptions
- ArgumentException
Thrown if
prkis not exactly the required size.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
Extract(HashAlgorithmName, Stream, ReadOnlySpan<byte>, SecureMemory<byte>)
Performs the extract step of HKDF (RFC 5869) using a stream as input keying material.
public static void Extract(HashAlgorithmName hashAlgorithmName, Stream ikm, ReadOnlySpan<byte> salt, SecureMemory<byte> prk)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material (IKM).
saltReadOnlySpan<byte>Optional salt value (can be empty).
prkSecureMemory<byte>Buffer to receive the pseudorandom key (32 bytes for SHA256 and 64 bytes for SHA512).
Exceptions
- ArgumentNullException
Thrown if
ikmis null.- ArgumentException
Thrown if
prklength is incorrect.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
Extract(HashAlgorithmName, Stream, ReadOnlySpan<byte>, Span<byte>)
Performs the extract step of HKDF (RFC 5869) using a stream as input keying material.
public static void Extract(HashAlgorithmName hashAlgorithmName, Stream ikm, ReadOnlySpan<byte> salt, Span<byte> prk)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material (IKM).
saltReadOnlySpan<byte>Optional salt value (can be empty).
prkSpan<byte>Buffer to receive the pseudorandom key (32 bytes for SHA256 and 64 bytes for SHA512).
Exceptions
- ArgumentNullException
Thrown if
ikmis null.- ArgumentException
Thrown if
prklength is incorrect.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
Extract(HashAlgorithmName, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Performs the extract step of HKDF (RFC 5869), using the specified hash algorithm.
public static void Extract(HashAlgorithmName hashAlgorithmName, ReadOnlySpan<byte> ikm, ReadOnlySpan<byte> salt, Span<byte> prk)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmReadOnlySpan<byte>Input keying material.
saltReadOnlySpan<byte>Optional salt value (can be empty).
prkSpan<byte>Buffer to receive the pseudorandom key (32 bytes for SHA256 and 64 bytes for SHA512).
Exceptions
- ArgumentException
Thrown if
prkis not exactly the required size.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
ExtractAsync(HashAlgorithmName, Stream, ReadOnlyMemory<byte>, SecureMemory<byte>, CancellationToken)
Asynchronously performs the extract step of HKDF (RFC 5869) using a stream as input keying material.
public static Task ExtractAsync(HashAlgorithmName hashAlgorithmName, Stream ikm, ReadOnlyMemory<byte> salt, SecureMemory<byte> prk, CancellationToken cancellationToken = default)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material (IKM).
saltReadOnlyMemory<byte>Optional salt value (can be empty).
prkSecureMemory<byte>Buffer to receive the pseudorandom key (32 bytes for SHA256 and 64 bytes for SHA512).
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- ArgumentNullException
Thrown if
ikmis null.- ArgumentException
Thrown if
prklength is incorrect.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.
ExtractAsync(HashAlgorithmName, Stream, ReadOnlyMemory<byte>, Memory<byte>, CancellationToken)
Asynchronously performs the extract step of HKDF (RFC 5869) using a stream as input keying material.
public static Task ExtractAsync(HashAlgorithmName hashAlgorithmName, Stream ikm, ReadOnlyMemory<byte> salt, Memory<byte> prk, CancellationToken cancellationToken = default)
Parameters
hashAlgorithmNameHashAlgorithmNameHash algorithm to use (SHA-256 or SHA-512).
ikmStreamStream of input keying material (IKM).
saltReadOnlyMemory<byte>Optional salt value (can be empty).
prkMemory<byte>Buffer to receive the pseudorandom key (32 bytes for SHA256 and 64 bytes for SHA512).
cancellationTokenCancellationTokenCancellation token.
Returns
Exceptions
- ArgumentNullException
Thrown if
ikmis null.- ArgumentException
Thrown if
prklength is incorrect.- NotSupportedException
Thrown if the hash algorithm is unsupported.
- LibSodiumException
Thrown if the underlying native call fails.