Class CryptoStreamXChaCha20
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Provides high-level access to the XChaCha20
stream cipher.
public static class CryptoStreamXChaCha20
- Inheritance
-
CryptoStreamXChaCha20
- Inherited Members
Fields
BlockLen
Block size of the keystream in bytes (64).
public static readonly int BlockLen
Field Value
KeyLen
Length of the secret key in bytes (32).
public static readonly int KeyLen
Field Value
NonceLen
Length of the nonce in bytes (24).
public static readonly int NonceLen
Field Value
Methods
Decrypt(SecureMemory<byte>, ReadOnlySpan<byte>, Stream, Stream)
Decrypts a stream using the XChaCha20
stream cipher.
public static void Decrypt(SecureMemory<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
key
SecureMemory<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce used during encryption. Must be exactly
24
bytes long.input
StreamThe stream containing ciphertext to decrypt.
output
StreamThe stream to receive the decrypted data.
Exceptions
- ArgumentException
Thrown if key or nonce are invalid.
Decrypt(SecureMemory<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, ulong)
Decrypts the given ciphertext using the XChaCha20
stream cipher, with the provided key and nonce.
public static void Decrypt(SecureMemory<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, Span<byte> plaintext, ulong initialCounter = 0)
Parameters
key
SecureMemory<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce used during encryption. Must be exactly
24
bytes long.ciphertext
ReadOnlySpan<byte>The ciphertext to decrypt (produced by XOR with keystream).
plaintext
Span<byte>The output buffer to receive the decrypted plaintext. Must be at least as long as
ciphertext
.initialCounter
ulongThe starting block index for the keystream (each block is 64 bytes). Use this to skip blocks, resume encryption, or decrypt from a specific offset. Defaults to 0.
Remarks
Decryption is symmetric: it re-generates the same keystream and XORs it with the ciphertext.
Exceptions
- ArgumentException
Thrown if any input length is invalid.
Decrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Stream, Stream)
Decrypts a stream using the XChaCha20
stream cipher.
public static void Decrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
key
ReadOnlySpan<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce used during encryption. Must be exactly
24
bytes long.input
StreamThe stream containing ciphertext to decrypt.
output
StreamThe stream to receive the decrypted data.
Exceptions
- ArgumentException
Thrown if key or nonce are invalid.
Decrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, ulong)
Decrypts the given ciphertext using the XChaCha20
stream cipher, with the provided key and nonce.
public static void Decrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, Span<byte> plaintext, ulong initialCounter = 0)
Parameters
key
ReadOnlySpan<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce used during encryption. Must be exactly
24
bytes long.ciphertext
ReadOnlySpan<byte>The ciphertext to decrypt (produced by XOR with keystream).
plaintext
Span<byte>The output buffer to receive the decrypted plaintext. Must be at least as long as
ciphertext
.initialCounter
ulongThe starting block index for the keystream (each block is 64 bytes). Use this to skip blocks, resume encryption, or decrypt from a specific offset. Defaults to 0.
Remarks
Decryption is symmetric: it re-generates the same keystream and XORs it with the ciphertext.
Exceptions
- ArgumentException
Thrown if any input length is invalid.
DecryptAsync(SecureMemory<byte>, ReadOnlyMemory<byte>, Stream, Stream, CancellationToken)
Asynchronously decrypts a stream using the XChaCha20
stream cipher.
public static Task DecryptAsync(SecureMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
key
SecureMemory<byte>The secret key. Must be exactly
32
bytes.nonce
ReadOnlyMemory<byte>The nonce used during encryption. Must be
24
bytes.input
StreamThe readable stream with ciphertext.
output
StreamThe writable stream to receive decrypted data.
cancellationToken
CancellationTokenOptional token to cancel the async operation.
Returns
- Task
A task that represents the asynchronous operation.
DecryptAsync(ReadOnlyMemory<byte>, ReadOnlyMemory<byte>, Stream, Stream, CancellationToken)
Asynchronously decrypts a stream using the XChaCha20
stream cipher.
public static Task DecryptAsync(ReadOnlyMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
key
ReadOnlyMemory<byte>The secret key. Must be exactly
32
bytes.nonce
ReadOnlyMemory<byte>The nonce used during encryption. Must be
24
bytes.input
StreamThe readable stream with ciphertext.
output
StreamThe writable stream to receive decrypted data.
cancellationToken
CancellationTokenOptional token to cancel the async operation.
Returns
- Task
A task that represents the asynchronous operation.
Encrypt(SecureMemory<byte>, ReadOnlySpan<byte>, Stream, Stream)
Encrypts a stream using the XChaCha20
stream cipher.
public static void Encrypt(SecureMemory<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
key
SecureMemory<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce. Must be exactly
24
bytes long.input
StreamThe stream containing plaintext data to encrypt.
output
StreamThe stream to receive the encrypted data.
Exceptions
- ArgumentException
Thrown if key or nonce are invalid.
Encrypt(SecureMemory<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, ulong)
Encrypts the given plaintext using the XChaCha20
stream cipher, with the provided key and nonce.
public static void Encrypt(SecureMemory<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, ulong initialCounter = 0)
Parameters
key
SecureMemory<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce. Must be exactly
24
bytes long. Must never be reused with the same key.plaintext
ReadOnlySpan<byte>The plaintext data to encrypt.
ciphertext
Span<byte>The output buffer to receive the encrypted data. Must be at least as long as
plaintext
.initialCounter
ulongThe starting block index for the keystream (each block is 64 bytes). Use this to skip blocks, resume encryption, or decrypt from a specific offset. Defaults to 0.
Remarks
This method XORs the plaintext with a keystream derived from the key and nonce.
Exceptions
- ArgumentException
Thrown if any input length is invalid.
Encrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Stream, Stream)
Encrypts a stream using the XChaCha20
stream cipher.
public static void Encrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
key
ReadOnlySpan<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce. Must be exactly
24
bytes long.input
StreamThe stream containing plaintext data to encrypt.
output
StreamThe stream to receive the encrypted data.
Exceptions
- ArgumentException
Thrown if key or nonce are invalid.
Encrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, ulong)
Encrypts the given plaintext using the XChaCha20
stream cipher, with the provided key and nonce.
public static void Encrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, ulong initialCounter = 0)
Parameters
key
ReadOnlySpan<byte>The secret key. Must be exactly
32
bytes long.nonce
ReadOnlySpan<byte>The nonce. Must be exactly
24
bytes long. Must never be reused with the same key.plaintext
ReadOnlySpan<byte>The plaintext data to encrypt.
ciphertext
Span<byte>The output buffer to receive the encrypted data. Must be at least as long as
plaintext
.initialCounter
ulongThe starting block index for the keystream (each block is 64 bytes). Use this to skip blocks, resume encryption, or decrypt from a specific offset. Defaults to 0.
Remarks
This method XORs the plaintext with a keystream derived from the key and nonce.
Exceptions
- ArgumentException
Thrown if any input length is invalid.
EncryptAsync(SecureMemory<byte>, ReadOnlyMemory<byte>, Stream, Stream, CancellationToken)
Asynchronously encrypts a stream using the XChaCha20
stream cipher.
public static Task EncryptAsync(SecureMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
key
SecureMemory<byte>The secret key. Must be exactly
32
bytes.nonce
ReadOnlyMemory<byte>The nonce. Must be exactly
24
bytes.input
StreamThe readable stream with plaintext data.
output
StreamThe writable stream to receive ciphertext.
cancellationToken
CancellationTokenOptional token to cancel the async operation.
Returns
- Task
A task that represents the asynchronous operation.
EncryptAsync(ReadOnlyMemory<byte>, ReadOnlyMemory<byte>, Stream, Stream, CancellationToken)
Asynchronously encrypts a stream using the XChaCha20
stream cipher.
public static Task EncryptAsync(ReadOnlyMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
key
ReadOnlyMemory<byte>The secret key. Must be exactly
32
bytes.nonce
ReadOnlyMemory<byte>The nonce. Must be exactly
24
bytes.input
StreamThe readable stream with plaintext data.
output
StreamThe writable stream to receive ciphertext.
cancellationToken
CancellationTokenOptional token to cancel the async operation.
Returns
- Task
A task that represents the asynchronous operation.
GenerateKeystream(SecureMemory<byte>, ReadOnlySpan<byte>, SecureMemory<byte>)
Generates a keystream of the specified length using the XChaCha20
stream cipher.
public static void GenerateKeystream(SecureMemory<byte> output, ReadOnlySpan<byte> nonce, SecureMemory<byte> key)
Parameters
output
SecureMemory<byte>The buffer to receive the generated keystream.
nonce
ReadOnlySpan<byte>The nonce. Must be exactly
24
bytes.key
SecureMemory<byte>The key. Must be exactly
32
bytes.
Exceptions
- ArgumentException
Thrown if
output
is empty or lengths are invalid.
GenerateKeystream(Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Generates a keystream of the specified length using the XChaCha20
stream cipher.
public static void GenerateKeystream(Span<byte> output, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> key)
Parameters
output
Span<byte>The buffer to receive the generated keystream.
nonce
ReadOnlySpan<byte>The nonce. Must be exactly
24
bytes.key
ReadOnlySpan<byte>The key. Must be exactly
32
bytes.
Exceptions
- ArgumentException
Thrown if
output
is empty or lengths are invalid.