Class CryptoStreamSalsa20
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Provides high-level access to the Salsa20 stream cipher.
public static class CryptoStreamSalsa20
- Inheritance
-
CryptoStreamSalsa20
- 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 (8).
public static readonly int NonceLen
Field Value
Methods
Decrypt(SecureMemory<byte>, ReadOnlySpan<byte>, Stream, Stream)
Decrypts a stream using the Salsa20 stream cipher.
public static void Decrypt(SecureMemory<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
keySecureMemory<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce used during encryption. Must be exactly
8bytes long.inputStreamThe stream containing ciphertext to decrypt.
outputStreamThe 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 Salsa20 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
keySecureMemory<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce used during encryption. Must be exactly
8bytes long.ciphertextReadOnlySpan<byte>The ciphertext to decrypt (produced by XOR with keystream).
plaintextSpan<byte>The output buffer to receive the decrypted plaintext. Must be at least as long as
ciphertext.initialCounterulongThe 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 Salsa20 stream cipher.
public static void Decrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
keyReadOnlySpan<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce used during encryption. Must be exactly
8bytes long.inputStreamThe stream containing ciphertext to decrypt.
outputStreamThe 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 Salsa20 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
keyReadOnlySpan<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce used during encryption. Must be exactly
8bytes long.ciphertextReadOnlySpan<byte>The ciphertext to decrypt (produced by XOR with keystream).
plaintextSpan<byte>The output buffer to receive the decrypted plaintext. Must be at least as long as
ciphertext.initialCounterulongThe 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 Salsa20 stream cipher.
public static Task DecryptAsync(SecureMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
keySecureMemory<byte>The secret key. Must be exactly
32bytes.nonceReadOnlyMemory<byte>The nonce used during encryption. Must be
8bytes.inputStreamThe readable stream with ciphertext.
outputStreamThe writable stream to receive decrypted data.
cancellationTokenCancellationTokenOptional 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 Salsa20 stream cipher.
public static Task DecryptAsync(ReadOnlyMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
keyReadOnlyMemory<byte>The secret key. Must be exactly
32bytes.nonceReadOnlyMemory<byte>The nonce used during encryption. Must be
8bytes.inputStreamThe readable stream with ciphertext.
outputStreamThe writable stream to receive decrypted data.
cancellationTokenCancellationTokenOptional 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 Salsa20 stream cipher.
public static void Encrypt(SecureMemory<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
keySecureMemory<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce. Must be exactly
8bytes long.inputStreamThe stream containing plaintext data to encrypt.
outputStreamThe 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 Salsa20 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
keySecureMemory<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce. Must be exactly
8bytes long. Must never be reused with the same key.plaintextReadOnlySpan<byte>The plaintext data to encrypt.
ciphertextSpan<byte>The output buffer to receive the encrypted data. Must be at least as long as
plaintext.initialCounterulongThe 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 Salsa20 stream cipher.
public static void Encrypt(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, Stream input, Stream output)
Parameters
keyReadOnlySpan<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce. Must be exactly
8bytes long.inputStreamThe stream containing plaintext data to encrypt.
outputStreamThe 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 Salsa20 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
keyReadOnlySpan<byte>The secret key. Must be exactly
32bytes long.nonceReadOnlySpan<byte>The nonce. Must be exactly
8bytes long. Must never be reused with the same key.plaintextReadOnlySpan<byte>The plaintext data to encrypt.
ciphertextSpan<byte>The output buffer to receive the encrypted data. Must be at least as long as
plaintext.initialCounterulongThe 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 Salsa20 stream cipher.
public static Task EncryptAsync(SecureMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
keySecureMemory<byte>The secret key. Must be exactly
32bytes.nonceReadOnlyMemory<byte>The nonce. Must be exactly
8bytes.inputStreamThe readable stream with plaintext data.
outputStreamThe writable stream to receive ciphertext.
cancellationTokenCancellationTokenOptional 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 Salsa20 stream cipher.
public static Task EncryptAsync(ReadOnlyMemory<byte> key, ReadOnlyMemory<byte> nonce, Stream input, Stream output, CancellationToken cancellationToken = default)
Parameters
keyReadOnlyMemory<byte>The secret key. Must be exactly
32bytes.nonceReadOnlyMemory<byte>The nonce. Must be exactly
8bytes.inputStreamThe readable stream with plaintext data.
outputStreamThe writable stream to receive ciphertext.
cancellationTokenCancellationTokenOptional 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 Salsa20 stream cipher.
public static void GenerateKeystream(SecureMemory<byte> output, ReadOnlySpan<byte> nonce, SecureMemory<byte> key)
Parameters
outputSecureMemory<byte>The buffer to receive the generated keystream.
nonceReadOnlySpan<byte>The nonce. Must be exactly
8bytes.keySecureMemory<byte>The key. Must be exactly
32bytes.
Exceptions
- ArgumentException
Thrown if
outputis empty or lengths are invalid.
GenerateKeystream(Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Generates a keystream of the specified length using the Salsa20 stream cipher.
public static void GenerateKeystream(Span<byte> output, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> key)
Parameters
outputSpan<byte>The buffer to receive the generated keystream.
nonceReadOnlySpan<byte>The nonce. Must be exactly
8bytes.keyReadOnlySpan<byte>The key. Must be exactly
32bytes.
Exceptions
- ArgumentException
Thrown if
outputis empty or lengths are invalid.