Class CryptoKeyDerivation
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Provides deterministic key derivation using libsodium's crypto_kdf_* API, based on the BLAKE2b hash function.
public static class CryptoKeyDerivation
- Inheritance
-
CryptoKeyDerivation
- Inherited Members
Fields
ContextLen
Length of the context in bytes (8).
public const int ContextLen = 8
Field Value
MasterKeyLen
Length of the master key in bytes (32).
public const int MasterKeyLen = 32
Field Value
MaxSubkeyLen
Maximum length of a derived subkey (64).
public const int MaxSubkeyLen = 64
Field Value
MinSubkeyLen
Minimum length of a derived subkey (16).
public const int MinSubkeyLen = 16
Field Value
Methods
DeriveSubkey(SecureMemory<byte>, SecureMemory<byte>, ulong, ReadOnlySpan<byte>)
Deterministically derives a subkey from a master key, context, and subkey ID. Uses the BLAKE2b hash function internally.
public static void DeriveSubkey(SecureMemory<byte> masterKey, SecureMemory<byte> subkey, ulong subkeyId, ReadOnlySpan<byte> context)
Parameters
masterKeySecureMemory<byte>The master key (32 bytes).
subkeySecureMemory<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextReadOnlySpan<byte>8-byte context describing the usage.
Exceptions
- ArgumentException
Thrown when
subkeyis out of bounds,contextis not 8 bytes, ormasterKeyis not 32 bytes.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(SecureMemory<byte>, SecureMemory<byte>, ulong, string)
Deterministically derives a subkey from a master key, using a context string whose UTF-8 representation is at most 8 bytes, and a subkey ID. If the string is shorter, it is padded with zeros. Uses the BLAKE2b hash function internally.
public static void DeriveSubkey(SecureMemory<byte> masterKey, SecureMemory<byte> subkey, ulong subkeyId, string context)
Parameters
masterKeySecureMemory<byte>The master key (32 bytes).
subkeySecureMemory<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextstringA string whose UTF-8 representation must be at most 8 bytes and describes the usage context.
Exceptions
- ArgumentNullException
Thrown when
contextis null.- ArgumentException
Thrown when
contextexceeds 8 UTF-8 bytes, orsubkeyormasterKeyare of invalid length.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(SecureMemory<byte>, ulong, ReadOnlySpan<byte>, SecureMemory<byte>)
Deterministically derives a subkey from a master key, context, and subkey ID. Uses the BLAKE2b hash function internally.
[Obsolete("Use the other overload instead. This will be removed in a future version.")]
public static void DeriveSubkey(SecureMemory<byte> subkey, ulong subkeyId, ReadOnlySpan<byte> context, SecureMemory<byte> masterKey)
Parameters
subkeySecureMemory<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextReadOnlySpan<byte>8-byte context describing the usage.
masterKeySecureMemory<byte>The master key (32 bytes).
Exceptions
- ArgumentException
Thrown when
subkeyis out of bounds,contextis not 8 bytes, ormasterKeyis not 32 bytes.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(SecureMemory<byte>, ulong, string, SecureMemory<byte>)
Deterministically derives a subkey from a master key, using a context string whose UTF-8 representation is at most 8 bytes, and a subkey ID. If the string is shorter, it is padded with zeros. Uses the BLAKE2b hash function internally.
[Obsolete("Use the other overload instead. This will be removed in a future version.")]
public static void DeriveSubkey(SecureMemory<byte> subkey, ulong subkeyId, string context, SecureMemory<byte> masterKey)
Parameters
subkeySecureMemory<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextstringA string whose UTF-8 representation must be at most 8 bytes and describes the usage context.
masterKeySecureMemory<byte>The master key (32 bytes).
Exceptions
- ArgumentNullException
Thrown when
contextis null.- ArgumentException
Thrown when
contextexceeds 8 UTF-8 bytes, orsubkeyormasterKeyare of invalid length.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(ReadOnlySpan<byte>, Span<byte>, ulong, ReadOnlySpan<byte>)
Deterministically derives a subkey from a master key, context, and subkey ID. Uses the BLAKE2b hash function internally.
public static void DeriveSubkey(ReadOnlySpan<byte> masterKey, Span<byte> subkey, ulong subkeyId, ReadOnlySpan<byte> context)
Parameters
masterKeyReadOnlySpan<byte>The master key (32 bytes).
subkeySpan<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextReadOnlySpan<byte>8-byte context describing the usage.
Exceptions
- ArgumentException
Thrown when
subkeyis out of bounds,contextis not 8 bytes, ormasterKeyis not 32 bytes.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(ReadOnlySpan<byte>, Span<byte>, ulong, string)
Deterministically derives a subkey from a master key, using a context string whose UTF-8 representation is at most 8 bytes, and a subkey ID. If the string is shorter, it is padded with zeros. Uses the BLAKE2b hash function internally.
public static void DeriveSubkey(ReadOnlySpan<byte> masterKey, Span<byte> subkey, ulong subkeyId, string context)
Parameters
masterKeyReadOnlySpan<byte>The master key (32 bytes).
subkeySpan<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextstringA string whose UTF-8 representation must be at most 8 bytes and describes the usage context.
Exceptions
- ArgumentNullException
Thrown when
contextis null.- ArgumentException
Thrown when
contextexceeds 8 UTF-8 bytes, orsubkeyormasterKeyare of invalid length.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(Span<byte>, ulong, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Deterministically derives a subkey from a master key, context, and subkey ID. Uses the BLAKE2b hash function internally.
[Obsolete("Use the other overload instead. This will be removed in a future version.")]
public static void DeriveSubkey(Span<byte> subkey, ulong subkeyId, ReadOnlySpan<byte> context, ReadOnlySpan<byte> masterKey)
Parameters
subkeySpan<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextReadOnlySpan<byte>8-byte context describing the usage.
masterKeyReadOnlySpan<byte>The master key (32 bytes).
Exceptions
- ArgumentException
Thrown when
subkeyis out of bounds,contextis not 8 bytes, ormasterKeyis not 32 bytes.- LibSodiumException
Thrown if the native key derivation fails.
DeriveSubkey(Span<byte>, ulong, string, ReadOnlySpan<byte>)
Deterministically derives a subkey from a master key, using a context string whose UTF-8 representation is at most 8 bytes, and a subkey ID. If the string is shorter, it is padded with zeros. Uses the BLAKE2b hash function internally.
[Obsolete("Use the other overload instead. This will be removed in a future version.")]
public static void DeriveSubkey(Span<byte> subkey, ulong subkeyId, string context, ReadOnlySpan<byte> masterKey)
Parameters
subkeySpan<byte>The buffer where the derived subkey will be written. Its length must be between 16 and 64 bytes.
subkeyIdulongThe identifier for the subkey (application-defined).
contextstringA string whose UTF-8 representation must be at most 8 bytes and describes the usage context.
masterKeyReadOnlySpan<byte>The master key (32 bytes).
Exceptions
- ArgumentNullException
Thrown when
contextis null.- ArgumentException
Thrown when
contextexceeds 8 UTF-8 bytes, orsubkeyormasterKeyare of invalid length.- LibSodiumException
Thrown if the native key derivation fails.
GenerateMasterKey(SecureMemory<byte>)
Fills the given buffer with a new random master key (32 bytes).
public static void GenerateMasterKey(SecureMemory<byte> masterKey)
Parameters
masterKeySecureMemory<byte>The buffer to fill. Must be 32 bytes.
Exceptions
- ArgumentException
Thrown when
masterKeyis not 32 bytes.
GenerateMasterKey(Span<byte>)
Fills the given buffer with a new random master key (32 bytes).
public static void GenerateMasterKey(Span<byte> masterKey)
Parameters
Exceptions
- ArgumentException
Thrown when
masterKeyis not 32 bytes.