Class MLDsa
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Dsa
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Represents an ML-DSA key (FIPS 204) and provides signing and verification.
public sealed class MLDsa : IDisposable
- Inheritance
-
MLDsa
- Implements
- Inherited Members
Remarks
The API shape mirrors System.Security.Cryptography.MLDsa from .NET 10 so code
written against the in-box type ports directly to older target frameworks, including
.NET Framework and .NET Standard 2.0.
An instance holds either a full key pair (created via GenerateKey(MLDsaAlgorithm), ImportMLDsaPrivateSeed(MLDsaAlgorithm, ReadOnlySpan<byte>), or ImportMLDsaPrivateKey(MLDsaAlgorithm, ReadOnlySpan<byte>)) or only a public key (via ImportMLDsaPublicKey(MLDsaAlgorithm, ReadOnlySpan<byte>)). Keys generated from a seed retain the 32-byte seed ξ, the compact storage form; keys imported from an expanded private key cannot export a seed. Private key material is zeroed when the instance is disposed.
External-μ signing (SignMu/VerifyMu) is not implemented.
Example usage:
using var signer = MLDsa.GenerateKey(MLDsaAlgorithm.MLDsa65);
byte[] publicKey = signer.ExportMLDsaPublicKey();
byte[] signature = signer.SignData(message);
using var verifier = MLDsa.ImportMLDsaPublicKey(MLDsaAlgorithm.MLDsa65, publicKey);
bool valid = verifier.VerifyData(message, signature);
Properties
Algorithm
Gets the ML-DSA parameter set of this key.
public MLDsaAlgorithm Algorithm { get; }
Property Value
IsSupported
Gets a value indicating whether ML-DSA is supported on the current platform.
public static bool IsSupported { get; }
Property Value
Remarks
Always true. This is a fully managed implementation, so unlike
System.Security.Cryptography.MLDsa.IsSupported it never depends on the
operating system providing ML-DSA.
Methods
Dispose()
Zeroizes the private seed and private key and releases the instance.
public void Dispose()
ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>, PbeOptions)
Exports this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, PbeOptions)
Exports this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<char>The password.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte>, PbeOptions)
Exports this key in a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public string ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- string
The PEM text, labelled
ENCRYPTED PRIVATE KEY.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>, PbeOptions)
Exports this key in a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public string ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<char>The password.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- string
The PEM text, labelled
ENCRYPTED PRIVATE KEY.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportMLDsaPrivateKey()
Exports the expanded private key.
public byte[] ExportMLDsaPrivateKey()
Returns
- byte[]
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportMLDsaPrivateKey(Span<byte>)
Exports the expanded private key into a buffer.
public void ExportMLDsaPrivateKey(Span<byte> destination)
Parameters
destinationSpan<byte>The buffer to receive the key; must be exactly PrivateKeySizeInBytes bytes.
Exceptions
- ArgumentException
destinationhas an incorrect length.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportMLDsaPrivateSeed()
Exports the 32-byte private seed ξ.
public byte[] ExportMLDsaPrivateSeed()
Returns
- byte[]
The private seed.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The key was not created from a seed.
ExportMLDsaPrivateSeed(Span<byte>)
Exports the 32-byte private seed ξ into a buffer.
public void ExportMLDsaPrivateSeed(Span<byte> destination)
Parameters
destinationSpan<byte>The buffer to receive the seed; must be exactly PrivateSeedSizeInBytes bytes.
Exceptions
- ArgumentException
destinationhas an incorrect length.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The key was not created from a seed.
ExportMLDsaPublicKey()
Exports the public key.
public byte[] ExportMLDsaPublicKey()
Returns
- byte[]
Exceptions
- ObjectDisposedException
The instance has been disposed.
ExportMLDsaPublicKey(Span<byte>)
Exports the public key into a buffer.
public void ExportMLDsaPublicKey(Span<byte> destination)
Parameters
destinationSpan<byte>The buffer to receive the key; must be exactly PublicKeySizeInBytes bytes.
Exceptions
- ArgumentException
destinationhas an incorrect length.- ObjectDisposedException
The instance has been disposed.
ExportPkcs8PrivateKey()
Exports this key in the PKCS#8 PrivateKeyInfo format.
public byte[] ExportPkcs8PrivateKey()
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportSubjectPublicKeyInfo()
Exports the public-key portion of this key in the X.509 SubjectPublicKeyInfo format.
public byte[] ExportSubjectPublicKeyInfo()
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
ExportSubjectPublicKeyInfoPem()
Exports the public-key portion of this key in a PEM-encoded SubjectPublicKeyInfo.
public string ExportSubjectPublicKeyInfoPem()
Returns
- string
The PEM text, labelled
PUBLIC KEY.
Exceptions
- ObjectDisposedException
The instance has been disposed.
GenerateKey(MLDsaAlgorithm)
Generates a new ML-DSA key pair.
public static MLDsa GenerateKey(MLDsaAlgorithm algorithm)
Parameters
algorithmMLDsaAlgorithmThe parameter set to generate a key for.
Returns
- MLDsa
A new instance holding the generated key pair and its private seed.
Exceptions
- ArgumentNullException
algorithmis null.- CryptographicException
The key pair failed the consistency test.
GenerateKey(MLDsaAlgorithm, bool)
Generates a new ML-DSA key pair, optionally skipping the pairwise consistency test.
public static MLDsa GenerateKey(MLDsaAlgorithm algorithm, bool pairwiseConsistencyTest)
Parameters
algorithmMLDsaAlgorithmThe parameter set to generate a key for.
pairwiseConsistencyTestbooltrue to verify the generated key pair with a sign/verify round trip; false to skip it.
Returns
- MLDsa
A new instance holding the generated key pair and its private seed.
Remarks
The pairwise consistency test verifies a freshly expanded key pair by signing and verifying one message, as FIPS 140-3 IG 10.3.A expects of a validated module. It is the dominant cost of key generation, because a sign is itself a rejection loop that runs several iterations on average.
It guards against a fault — bad memory, a bit flip, a miscompiled build — producing a key pair that does not round-trip. It cannot catch an implementation bug, since both halves of the test would be wrong in the same way. Disable it only where that trade is understood and key generation throughput actually matters; the default on the BCL-shaped overloads keeps it enabled.
Exceptions
- ArgumentNullException
algorithmis null.- CryptographicException
The key pair failed the consistency test.
GetPkcs8PrivateKeyPemSize()
Gets the exact number of characters TryExportPkcs8PrivateKeyPem(Span<char>, out int) writes for this key.
public int GetPkcs8PrivateKeyPemSize()
Returns
- int
The required buffer length, in characters.
Remarks
The counterpart of System.Security.Cryptography.PemEncoding.GetEncodedSize, which is
not available on every target framework this library supports. Rent a buffer of this size,
call TryExportPkcs8PrivateKeyPem(Span<char>, out int), and clear the buffer when finished.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
GetSubjectPublicKeyInfoPemSize()
Gets the exact number of characters TryExportSubjectPublicKeyInfoPem(Span<char>, out int) writes.
public int GetSubjectPublicKeyInfoPemSize()
Returns
- int
The required buffer length, in characters.
Exceptions
- ObjectDisposedException
The instance has been disposed.
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Imports an ML-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
public static MLDsa ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> password, ReadOnlySpan<byte> source)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
sourceReadOnlySpan<byte>The DER-encoded EncryptedPrivateKeyInfo.
Returns
- MLDsa
The imported key.
Exceptions
- CryptographicException
The password is wrong, or the structure is malformed or is not ML-DSA.
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, ReadOnlySpan<byte>)
Imports an ML-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
public static MLDsa ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, ReadOnlySpan<byte> source)
Parameters
passwordReadOnlySpan<char>The password, encoded per the scheme in the structure.
sourceReadOnlySpan<byte>The DER-encoded EncryptedPrivateKeyInfo.
Returns
- MLDsa
The imported key.
Exceptions
- CryptographicException
The password is wrong, or the structure is malformed or is not ML-DSA.
ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<byte>)
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
public static MLDsa ImportFromEncryptedPem(ReadOnlySpan<char> source, ReadOnlySpan<byte> passwordBytes)
Parameters
sourceReadOnlySpan<char>The PEM text.
passwordBytesReadOnlySpan<byte>The password, used verbatim as the key derivation input.
Returns
- MLDsa
The imported key.
Exceptions
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<char>)
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
public static MLDsa ImportFromEncryptedPem(ReadOnlySpan<char> source, ReadOnlySpan<char> password)
Parameters
sourceReadOnlySpan<char>The PEM text.
passwordReadOnlySpan<char>The password.
Returns
- MLDsa
The imported key.
Exceptions
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromEncryptedPem(string, byte[])
Imports an ML-DSA key from an encrypted RFC 7468 PEM-encoded string.
public static MLDsa ImportFromEncryptedPem(string source, byte[] passwordBytes)
Parameters
sourcestringThe PEM text.
passwordBytesbyte[]The password, used verbatim as the key derivation input.
Returns
- MLDsa
The imported key.
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromPem(ReadOnlySpan<char>)
Imports an ML-DSA key from an RFC 7468 PEM-encoded string.
public static MLDsa ImportFromPem(ReadOnlySpan<char> source)
Parameters
sourceReadOnlySpan<char>The PEM text.
Returns
- MLDsa
The imported key.
Exceptions
- ArgumentException
sourcecontains no PEM block with a recognized label, or more than one.
ImportMLDsaPrivateKey(MLDsaAlgorithm, byte[])
Imports an expanded ML-DSA private key.
public static MLDsa ImportMLDsaPrivateKey(MLDsaAlgorithm algorithm, byte[] source)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourcebyte[]The expanded private key.
Returns
- MLDsa
A new instance holding the key pair.
Remarks
A key imported this way holds no private seed, so ExportMLDsaPrivateSeed() is unavailable. The public key is reconstructed from the private key and validated against the embedded hash tr = H(pk); a mismatch rejects the import.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The embedded public key hash does not match the reconstructed public key.
ImportMLDsaPrivateKey(MLDsaAlgorithm, ReadOnlySpan<byte>)
Imports an expanded ML-DSA private key.
public static MLDsa ImportMLDsaPrivateKey(MLDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The expanded private key.
Returns
- MLDsa
A new instance holding the key pair.
Remarks
A key imported this way holds no private seed, so ExportMLDsaPrivateSeed() is unavailable. The public key is reconstructed from the private key and validated against the embedded hash tr = H(pk); a mismatch rejects the import.
Exceptions
- CryptographicException
The embedded public key hash does not match the reconstructed public key.
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.
ImportMLDsaPrivateSeed(MLDsaAlgorithm, byte[])
Imports an ML-DSA private seed ξ and expands it into a key pair.
public static MLDsa ImportMLDsaPrivateSeed(MLDsaAlgorithm algorithm, byte[] source)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourcebyte[]The 32-byte private seed.
Returns
- MLDsa
A new instance holding the key pair and the seed.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key pair failed the consistency test.
ImportMLDsaPrivateSeed(MLDsaAlgorithm, ReadOnlySpan<byte>)
Imports an ML-DSA private seed ξ and expands it into a key pair.
public static MLDsa ImportMLDsaPrivateSeed(MLDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The 32-byte private seed.
Returns
- MLDsa
A new instance holding the key pair and the seed.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key pair failed the consistency test.
ImportMLDsaPrivateSeed(MLDsaAlgorithm, ReadOnlySpan<byte>, bool)
Imports an ML-DSA private seed ξ and expands it into a key pair, optionally skipping the pairwise consistency test.
public static MLDsa ImportMLDsaPrivateSeed(MLDsaAlgorithm algorithm, ReadOnlySpan<byte> source, bool pairwiseConsistencyTest)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The 32-byte private seed.
pairwiseConsistencyTestbool
Returns
- MLDsa
A new instance holding the key pair and the seed.
Remarks
The pairwise consistency test verifies a freshly expanded key pair by signing and verifying one message, as FIPS 140-3 IG 10.3.A expects of a validated module. It is the dominant cost of key generation, because a sign is itself a rejection loop that runs several iterations on average.
It guards against a fault — bad memory, a bit flip, a miscompiled build — producing a key pair that does not round-trip. It cannot catch an implementation bug, since both halves of the test would be wrong in the same way. Disable it only where that trade is understood and key generation throughput actually matters; the default on the BCL-shaped overloads keeps it enabled.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key pair failed the consistency test.
ImportMLDsaPublicKey(MLDsaAlgorithm, byte[])
Imports an ML-DSA public key.
public static MLDsa ImportMLDsaPublicKey(MLDsaAlgorithm algorithm, byte[] source)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourcebyte[]The public key.
Returns
- MLDsa
A new instance holding only the public key; it can verify but not sign.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.
ImportMLDsaPublicKey(MLDsaAlgorithm, ReadOnlySpan<byte>)
Imports an ML-DSA public key.
public static MLDsa ImportMLDsaPublicKey(MLDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmMLDsaAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The public key.
Returns
- MLDsa
A new instance holding only the public key; it can verify but not sign.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.
ImportPkcs8PrivateKey(byte[])
Imports an ML-DSA private key from a PKCS#8 PrivateKeyInfo structure.
public static MLDsa ImportPkcs8PrivateKey(byte[] source)
Parameters
sourcebyte[]The DER-encoded PrivateKeyInfo.
Returns
- MLDsa
The imported key.
Exceptions
- ArgumentNullException
sourceis null.- CryptographicException
The structure is malformed or is not ML-DSA.
ImportPkcs8PrivateKey(ReadOnlySpan<byte>)
Imports an ML-DSA private key from a PKCS#8 PrivateKeyInfo structure.
public static MLDsa ImportPkcs8PrivateKey(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The DER-encoded PrivateKeyInfo.
Returns
- MLDsa
The imported key.
Exceptions
- CryptographicException
The structure is malformed or is not ML-DSA.
ImportSubjectPublicKeyInfo(byte[])
Imports an ML-DSA public key from an X.509 SubjectPublicKeyInfo structure.
public static MLDsa ImportSubjectPublicKeyInfo(byte[] source)
Parameters
sourcebyte[]The DER-encoded SubjectPublicKeyInfo.
Returns
- MLDsa
A verify-only instance.
Exceptions
- ArgumentNullException
sourceis null.- CryptographicException
The structure is malformed or is not ML-DSA.
ImportSubjectPublicKeyInfo(ReadOnlySpan<byte>)
Imports an ML-DSA public key from an X.509 SubjectPublicKeyInfo structure.
public static MLDsa ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The DER-encoded SubjectPublicKeyInfo.
Returns
- MLDsa
A verify-only instance.
Exceptions
- CryptographicException
The structure is malformed or is not ML-DSA.
SignData(byte[], byte[]?)
Signs data using the hedged (randomized) variant of ML-DSA.
public byte[] SignData(byte[] data, byte[]? context = null)
Parameters
databyte[]The data to sign.
contextbyte[]The optional context string (at most 255 bytes), or null.
Returns
- byte[]
The signature.
Exceptions
- ArgumentNullException
datais null.- ArgumentException
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
SignData(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Signs data using the hedged (randomized) variant of ML-DSA.
public byte[] SignData(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context = default)
Parameters
dataReadOnlySpan<byte>The data to sign.
contextReadOnlySpan<byte>The optional context string (at most 255 bytes).
Returns
- byte[]
The signature.
Exceptions
- ArgumentException
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
SignData(ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>)
Signs data into a caller-provided buffer using the hedged (randomized) variant of ML-DSA.
public void SignData(ReadOnlySpan<byte> data, Span<byte> destination, ReadOnlySpan<byte> context = default)
Parameters
dataReadOnlySpan<byte>The data to sign.
destinationSpan<byte>The buffer to receive the signature; must be exactly SignatureSizeInBytes bytes.
contextReadOnlySpan<byte>The optional context string (at most 255 bytes).
Exceptions
- ArgumentException
A parameter has an invalid size.
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
SignPreHash(byte[], string, byte[]?)
Signs a pre-computed message digest using HashML-DSA (FIPS 204 §5.4).
public byte[] SignPreHash(byte[] hash, string hashAlgorithmOid, byte[]? context = null)
Parameters
hashbyte[]The pre-computed digest PH(M).
hashAlgorithmOidstringThe dotted-decimal OID of the pre-hash function, e.g.
2.16.840.1.101.3.4.2.3for SHA-512.contextbyte[]The optional context string (at most 255 bytes), or null.
Returns
- byte[]
The signature.
Remarks
The caller computes PH(M) with an approved hash or XOF and passes the digest with its OID; the signature binds the pre-hash function via M′ = 0x01 ‖ |ctx| ‖ ctx ‖ OID ‖ PH(M). Pre-hash signatures are never interchangeable with pure ML-DSA signatures over the same message.
Exceptions
- ArgumentNullException
hashorhashAlgorithmOidis null.- ArgumentException
The OID is not approved, the digest length does not match it, or
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
SignPreHash(ReadOnlySpan<byte>, Span<byte>, string, ReadOnlySpan<byte>)
Signs a pre-computed message digest into a caller-provided buffer using HashML-DSA (FIPS 204 §5.4).
public void SignPreHash(ReadOnlySpan<byte> hash, Span<byte> destination, string hashAlgorithmOid, ReadOnlySpan<byte> context = default)
Parameters
hashReadOnlySpan<byte>The pre-computed digest PH(M).
destinationSpan<byte>The buffer to receive the signature; must be exactly SignatureSizeInBytes bytes.
hashAlgorithmOidstringThe dotted-decimal OID of the pre-hash function.
contextReadOnlySpan<byte>The optional context string (at most 255 bytes).
Remarks
Note the parameter order, which this type inherits from the in-box MLDsa: the
second parameter is the destination buffer, whereas on SignData(byte[], byte[]?)
the second parameter is the context. Call sites that pass arrays should spell out
new ReadOnlySpan<byte>(…) and new Span<byte>(…) so the intended
overload is unambiguous.
Exceptions
- ArgumentNullException
hashAlgorithmOidis null.- ArgumentException
A parameter has an invalid size, or the OID is not an approved pre-hash function.
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>, PbeOptions, Span<byte>, out int)
Attempts to export this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> password, PbeOptions pbeOptions, Span<byte> destination, out int bytesWritten)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, PbeOptions, Span<byte>, out int)
Attempts to export this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, PbeOptions pbeOptions, Span<byte> destination, out int bytesWritten)
Parameters
passwordReadOnlySpan<char>The password.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte>, PbeOptions, Span<char>, out int)
Attempts to export this key as a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public bool TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte> password, PbeOptions pbeOptions, Span<char> destination, out int charsWritten)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<char>The buffer to receive the text.
charsWrittenintThe number of characters written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>, PbeOptions, Span<char>, out int)
Attempts to export this key as a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public bool TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char> password, PbeOptions pbeOptions, Span<char> destination, out int charsWritten)
Parameters
passwordReadOnlySpan<char>The password, encoded per the scheme being written.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<char>The buffer to receive the text.
charsWrittenintThe number of characters written.
Returns
Remarks
There is no size helper for this member: measuring the output means running the key derivation function, which at a realistic iteration count is the whole cost of the export. Use ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>, PbeOptions) - safe, because the payload is ciphertext - or grow a buffer until this succeeds.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportPkcs8PrivateKey(Span<byte>, out int)
Attempts to export this key in the PKCS#8 PrivateKeyInfo format.
public bool TryExportPkcs8PrivateKey(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportPkcs8PrivateKeyPem(Span<char>, out int)
Attempts to export this key as a PEM-encoded PKCS#8 PrivateKeyInfo, writing into a buffer the caller owns and can erase.
public bool TryExportPkcs8PrivateKeyPem(Span<char> destination, out int charsWritten)
Parameters
destinationSpan<char>The buffer to receive the text, labelled
PRIVATE KEY.charsWrittenintThe number of characters written.
Returns
Remarks
There is no allocating ExportPkcs8PrivateKeyPem(). The text this produces is
the private key, and a string holding it could never be erased - so the only
export this type offers is one that writes where the caller can clear it afterwards. Size
the buffer with GetPkcs8PrivateKeyPemSize().
When the buffer is too small nothing is written and charsWritten is zero.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportSubjectPublicKeyInfo(Span<byte>, out int)
Attempts to export the public-key portion of this key as a SubjectPublicKeyInfo.
public bool TryExportSubjectPublicKeyInfo(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
TryExportSubjectPublicKeyInfoPem(Span<char>, out int)
Attempts to export this key as a PEM-encoded SubjectPublicKeyInfo.
public bool TryExportSubjectPublicKeyInfoPem(Span<char> destination, out int charsWritten)
Parameters
destinationSpan<char>The buffer to receive the text, labelled
PUBLIC KEY.charsWrittenintThe number of characters written.
Returns
Remarks
A public key is not secret, so ExportSubjectPublicKeyInfoPem() remains available
and is usually the simpler call. This overload exists for parity with
AsymmetricAlgorithm, which offers the same pair, and for callers avoiding allocation.
Exceptions
- ObjectDisposedException
The instance has been disposed.
VerifyData(byte[], byte[], byte[]?)
Verifies a signature over data.
public bool VerifyData(byte[] data, byte[] signature, byte[]? context = null)
Parameters
databyte[]The signed data.
signaturebyte[]The signature to verify.
contextbyte[]The context string used when signing (at most 255 bytes), or null.
Returns
- bool
True when the signature is valid; false for invalid signatures, including malformed lengths.
Exceptions
- ArgumentNullException
dataorsignatureis null.- ArgumentException
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.
VerifyData(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Verifies a signature over data.
public bool VerifyData(ReadOnlySpan<byte> data, ReadOnlySpan<byte> signature, ReadOnlySpan<byte> context = default)
Parameters
dataReadOnlySpan<byte>The signed data.
signatureReadOnlySpan<byte>The signature to verify.
contextReadOnlySpan<byte>The context string used when signing (at most 255 bytes).
Returns
- bool
True when the signature is valid; false for invalid signatures, including malformed lengths.
Exceptions
- ArgumentException
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.
VerifyPreHash(byte[], byte[], string, byte[]?)
Verifies a HashML-DSA signature over a pre-computed message digest (FIPS 204 §5.4).
public bool VerifyPreHash(byte[] hash, byte[] signature, string hashAlgorithmOid, byte[]? context = null)
Parameters
hashbyte[]The pre-computed digest PH(M).
signaturebyte[]The signature to verify.
hashAlgorithmOidstringThe dotted-decimal OID of the pre-hash function used when signing.
contextbyte[]The context string used when signing (at most 255 bytes), or null.
Returns
- bool
True when the signature is valid; false for invalid signatures, including malformed lengths.
Exceptions
- ArgumentNullException
hash,signatureorhashAlgorithmOidis null.- ArgumentException
The OID is not approved, the digest length does not match it, or
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.
VerifyPreHash(ReadOnlySpan<byte>, ReadOnlySpan<byte>, string, ReadOnlySpan<byte>)
Verifies a HashML-DSA signature over a pre-computed message digest (FIPS 204 §5.4).
public bool VerifyPreHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature, string hashAlgorithmOid, ReadOnlySpan<byte> context = default)
Parameters
hashReadOnlySpan<byte>The pre-computed digest PH(M).
signatureReadOnlySpan<byte>The signature to verify.
hashAlgorithmOidstringThe dotted-decimal OID of the pre-hash function used when signing.
contextReadOnlySpan<byte>The context string used when signing (at most 255 bytes).
Returns
- bool
True when the signature is valid; false for invalid signatures, including malformed lengths.
Exceptions
- ArgumentNullException
hashAlgorithmOidis null.- ArgumentException
The OID is not approved, the digest length does not match it, or
contextis longer than 255 bytes.- ObjectDisposedException
The instance has been disposed.