Class SlhDsa
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Dsa
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Represents an SLH-DSA key (FIPS 205) and provides signing and verification.
public sealed class SlhDsa : IDisposable
- Inheritance
-
SlhDsa
- Implements
- Inherited Members
Remarks
The API shape mirrors System.Security.Cryptography.SlhDsa 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.
SLH-DSA is a stateless hash-based signature scheme: its security rests only on the
underlying hash functions, making it the conservative choice for long-lived keys
(roots of trust, firmware and code signing). Signing is expensive by design —
prefer the f (fast) parameter sets unless minimal signature size matters more
than signing time; verification is fast for all sets.
An instance holds either a full key pair (via GenerateKey(SlhDsaAlgorithm) or ImportSlhDsaPrivateKey(SlhDsaAlgorithm, ReadOnlySpan<byte>)) or only a public key (via ImportSlhDsaPublicKey(SlhDsaAlgorithm, ReadOnlySpan<byte>)). The 4n-byte private key is itself the compact storage form (SK.seed ‖ SK.prf ‖ PK.seed ‖ PK.root); there is no separate private seed. Private key material is zeroed when the instance is disposed.
Example usage:
using var signer = SlhDsa.GenerateKey(SlhDsaAlgorithm.SlhDsaShake128f);
byte[] publicKey = signer.ExportSlhDsaPublicKey();
byte[] signature = signer.SignData(message);
using var verifier = SlhDsa.ImportSlhDsaPublicKey(SlhDsaAlgorithm.SlhDsaShake128f, publicKey);
bool valid = verifier.VerifyData(message, signature);
Properties
Algorithm
Gets the SLH-DSA parameter set of this key.
public SlhDsaAlgorithm Algorithm { get; }
Property Value
IsSupported
Gets a value indicating whether SLH-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.SlhDsa.IsSupported it never depends on the
operating system providing SLH-DSA.
Methods
Dispose()
Zeroizes the 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.
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.
ExportSlhDsaPrivateKey()
Exports the current key in the FIPS 205 private key format (SK.seed ‖ SK.prf ‖ PK.seed ‖ PK.root).
public byte[] ExportSlhDsaPrivateKey()
Returns
- byte[]
The private key.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportSlhDsaPrivateKey(Span<byte>)
Exports the current key in the FIPS 205 private key format into a buffer.
public void ExportSlhDsaPrivateKey(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.
ExportSlhDsaPublicKey()
Exports the public-key portion of the current key in the FIPS 205 public key format (PK.seed ‖ PK.root).
public byte[] ExportSlhDsaPublicKey()
Returns
- byte[]
The public key.
Exceptions
- ObjectDisposedException
The instance has been disposed.
ExportSlhDsaPublicKey(Span<byte>)
Exports the public-key portion of the current key into a buffer.
public void ExportSlhDsaPublicKey(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.
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(SlhDsaAlgorithm)
Generates a new SLH-DSA key pair.
public static SlhDsa GenerateKey(SlhDsaAlgorithm algorithm)
Parameters
algorithmSlhDsaAlgorithmThe parameter set to generate a key for.
Returns
- SlhDsa
A new instance holding the generated key pair.
Exceptions
- ArgumentNullException
algorithmis null.- CryptographicException
The key pair failed the consistency test.
GenerateKey(SlhDsaAlgorithm, bool)
Generates a new SLH-DSA key pair, optionally skipping the pairwise consistency test.
public static SlhDsa GenerateKey(SlhDsaAlgorithm algorithm, bool pairwiseConsistencyTest)
Parameters
algorithmSlhDsaAlgorithmThe 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
- SlhDsa
A new instance holding the generated key pair.
Remarks
The pairwise consistency test verifies a freshly generated key pair by signing and verifying one message, as FIPS 140-3 IG 10.3.A expects of a validated module.
It is disproportionately expensive for SLH-DSA. A signature is on the order of 10⁶
hash invocations for the s parameter sets, so the check does not merely add to
key generation — it costs orders of magnitude more than generating the key did.
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 overload 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 SLH-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
public static SlhDsa 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
- SlhDsa
The imported key.
Exceptions
- CryptographicException
The password is wrong, or the structure is malformed or is not SLH-DSA.
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, ReadOnlySpan<byte>)
Imports an SLH-DSA private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
public static SlhDsa 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
- SlhDsa
The imported key.
Exceptions
- CryptographicException
The password is wrong, or the structure is malformed or is not SLH-DSA.
ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<byte>)
Imports an SLH-DSA key from an encrypted RFC 7468 PEM-encoded string.
public static SlhDsa 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
- SlhDsa
The imported key.
Exceptions
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<char>)
Imports an SLH-DSA key from an encrypted RFC 7468 PEM-encoded string.
public static SlhDsa ImportFromEncryptedPem(ReadOnlySpan<char> source, ReadOnlySpan<char> password)
Parameters
sourceReadOnlySpan<char>The PEM text.
passwordReadOnlySpan<char>The password.
Returns
- SlhDsa
The imported key.
Exceptions
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromEncryptedPem(string, byte[])
Imports an SLH-DSA key from an encrypted RFC 7468 PEM-encoded string.
public static SlhDsa ImportFromEncryptedPem(string source, byte[] passwordBytes)
Parameters
sourcestringThe PEM text.
passwordBytesbyte[]The password, used verbatim as the key derivation input.
Returns
- SlhDsa
The imported key.
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromPem(ReadOnlySpan<char>)
Imports an SLH-DSA key from an RFC 7468 PEM-encoded string.
public static SlhDsa ImportFromPem(ReadOnlySpan<char> source)
Parameters
sourceReadOnlySpan<char>The PEM text.
Returns
- SlhDsa
The imported key.
Exceptions
- ArgumentException
sourcecontains no PEM block with a recognized label, or more than one.
ImportPkcs8PrivateKey(byte[])
Imports an SLH-DSA private key from a PKCS#8 PrivateKeyInfo structure.
public static SlhDsa ImportPkcs8PrivateKey(byte[] source)
Parameters
sourcebyte[]The DER-encoded PrivateKeyInfo.
Returns
- SlhDsa
The imported key.
Exceptions
- ArgumentNullException
sourceis null.- CryptographicException
The structure is malformed or is not SLH-DSA.
ImportPkcs8PrivateKey(ReadOnlySpan<byte>)
Imports an SLH-DSA private key from a PKCS#8 PrivateKeyInfo structure.
public static SlhDsa ImportPkcs8PrivateKey(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The DER-encoded PrivateKeyInfo.
Returns
- SlhDsa
The imported key.
Exceptions
- CryptographicException
The structure is malformed or is not SLH-DSA.
ImportSlhDsaPrivateKey(SlhDsaAlgorithm, byte[])
Imports an SLH-DSA private key in the FIPS 205 private key format.
public static SlhDsa ImportSlhDsaPrivateKey(SlhDsaAlgorithm algorithm, byte[] source)
Parameters
algorithmSlhDsaAlgorithmThe parameter set of the key.
sourcebyte[]The 4n-byte private key.
Returns
- SlhDsa
A new instance holding the key pair.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.
ImportSlhDsaPrivateKey(SlhDsaAlgorithm, ReadOnlySpan<byte>)
Imports an SLH-DSA private key in the FIPS 205 private key format.
public static SlhDsa ImportSlhDsaPrivateKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmSlhDsaAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The 4n-byte private key.
Returns
- SlhDsa
A new instance holding the key pair.
Remarks
The public key (PK.seed ‖ PK.root) is embedded in the private key and extracted on import; no expensive consistency recomputation is performed.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.
ImportSlhDsaPublicKey(SlhDsaAlgorithm, byte[])
Imports an SLH-DSA public key in the FIPS 205 public key format.
public static SlhDsa ImportSlhDsaPublicKey(SlhDsaAlgorithm algorithm, byte[] source)
Parameters
algorithmSlhDsaAlgorithmThe parameter set of the key.
sourcebyte[]The 2n-byte public key.
Returns
- SlhDsa
A new instance holding only the public key; it can verify but not sign.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.
ImportSlhDsaPublicKey(SlhDsaAlgorithm, ReadOnlySpan<byte>)
Imports an SLH-DSA public key in the FIPS 205 public key format.
public static SlhDsa ImportSlhDsaPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmSlhDsaAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The 2n-byte public key.
Returns
- SlhDsa
A new instance holding only the public key; it can verify but not sign.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.
ImportSubjectPublicKeyInfo(byte[])
Imports an SLH-DSA public key from an X.509 SubjectPublicKeyInfo structure.
public static SlhDsa ImportSubjectPublicKeyInfo(byte[] source)
Parameters
sourcebyte[]The DER-encoded SubjectPublicKeyInfo.
Returns
- SlhDsa
A verify-only instance.
Exceptions
- ArgumentNullException
sourceis null.- CryptographicException
The structure is malformed or is not SLH-DSA.
ImportSubjectPublicKeyInfo(ReadOnlySpan<byte>)
Imports an SLH-DSA public key from an X.509 SubjectPublicKeyInfo structure.
public static SlhDsa ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The DER-encoded SubjectPublicKeyInfo.
Returns
- SlhDsa
A verify-only instance.
Exceptions
- CryptographicException
The structure is malformed or is not SLH-DSA.
SignData(byte[], byte[]?)
Signs data using the hedged (randomized) variant of SLH-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.
Remarks
Mirrors the in-box overload, and inherits its shape: the second parameter is the context, not a destination buffer. Callers holding a pre-allocated signature buffer want the span overload and must say so explicitly — passing two arrays binds here and silently treats the signature buffer as a context string.
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 SLH-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 SLH-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 HashSLH-DSA (FIPS 205 §10.2).
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.1for SHA-256.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 SLH-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 HashSLH-DSA (FIPS 205 §10.2).
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 SlhDsa: 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 HashSLH-DSA signature over a pre-computed message digest (FIPS 205 §10.2).
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 HashSLH-DSA signature over a pre-computed message digest (FIPS 205 §10.2).
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.