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
key
ReadOnlySpan<byte>A 32-byte decryption key.
publicNonce
ReadOnlySpan<byte>A 24-byte public nonce used for decryption. It must match the nonce used during encryption.
authenticationData
ReadOnlySpan<byte>Optional additional data that was authenticated during encryption but not encrypted.
ciphertext
ReadOnlySpan<byte>The ciphertext and authentication tag. The buffer should be at least the length of the plaintext plus 16 bytes.
plaintext
Span<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
key
ReadOnlySpan<byte>A 32-byte encryption key.
publicNonce
ReadOnlySpan<byte>A 24-byte public nonce used for encryption, ensuring uniqueness for each encryption operation
authenticationData
ReadOnlySpan<byte>Optional additional data to authenticate, which is not encrypted.
plaintext
ReadOnlySpan<byte>The plaintext to be encrypted.
ciphertext
Span<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.