Class XChaCha20Poly1305
- Namespace
- NLightning.Infrastructure.Crypto.Ciphers
- Assembly
- NLightning.Infrastructure.dll
AEAD_XCHACHA20_POLY1305 from draft-irtf-cfrg-xchacha-03. The 96-bit nonce is formed by encoding 32 bits of zeros followed by little-endian encoding of n.
public sealed class XChaCha20Poly1305 : IDisposable
- Inheritance
-
XChaCha20Poly1305
- Implements
- Inherited Members
Constructors
XChaCha20Poly1305()
public XChaCha20Poly1305()
Methods
Decrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Decrypts ciphertext using a 32-byte cipher key, 24-byte nonce, and optional authentication data. Writes the resulting plaintext into the provided plaintext buffer. Returns the length of the plaintext.
public int Decrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> publicNonce, ReadOnlySpan<byte> authenticationData, ReadOnlySpan<byte> ciphertext, Span<byte> plaintext)
Parameters
keyReadOnlySpan<byte>A 32-byte decryption key.
publicNonceReadOnlySpan<byte>A 24-byte public nonce used for decryption. It must match the nonce used during encryption.
authenticationDataReadOnlySpan<byte>Optional additional data that was authenticated during encryption but not encrypted.
ciphertextReadOnlySpan<byte>The ciphertext and authentication tag. The buffer should be at least the length of the plaintext plus 16 bytes.
plaintextSpan<byte>A buffer to store the resulting plaintext. It must be large enough to store the decrypted data.
Returns
- int
The total number of bytes written to the plaintext buffer.
Exceptions
- CryptographicException
Thrown when the decryption process fails.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Encrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Encrypts plaintext using a cipher key of 32 bytes, 24-byte nonce, and optional authentication data. Writes the resulting ciphertext and authentication tag into the provided ciphertext buffer. Returns the length of the ciphertext, including the authentication tag.
public int Encrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> publicNonce, ReadOnlySpan<byte> authenticationData, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext)
Parameters
keyReadOnlySpan<byte>A 32-byte encryption key.
publicNonceReadOnlySpan<byte>A 24-byte public nonce used for encryption, ensuring uniqueness for each encryption operation
authenticationDataReadOnlySpan<byte>Optional additional data to authenticate, which is not encrypted.
plaintextReadOnlySpan<byte>The plaintext to be encrypted.
ciphertextSpan<byte>A buffer to store the resulting ciphertext and authentication tag. Must be large enough to hold plaintext length plus 16 bytes.
Returns
- int
The total number of bytes written to the ciphertext buffer, including the authentication tag.
Exceptions
- CryptographicException
Thrown when the encryption process fails.