Class ChaCha20
Class for ChaCha20 encryption / decryption
Implements
Inherited Members
Namespace: CSChaCha20
Assembly: .dll
Syntax
public sealed class ChaCha20 : IDisposable
Constructors
| Edit this page View SourceChaCha20(byte[], byte[], uint)
Set up a new ChaCha20 state. The lengths of the given parameters are checked before encryption happens.
Declaration
public ChaCha20(byte[] key, byte[] nonce, uint counter)
Parameters
Type | Name | Description |
---|---|---|
byte[] | key | A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit little-endian integers |
byte[] | nonce | A 12-byte (96-bit) nonce, treated as a concatenation of three 32-bit little-endian integers |
uint | counter | A 4-byte (32-bit) block counter, treated as a 32-bit little-endian integer |
Remarks
See ChaCha20 Spec Section 2.4 for a detailed description of the inputs.
ChaCha20(ReadOnlySpan<byte>, ReadOnlySpan<byte>, uint)
Set up a new ChaCha20 state. The lengths of the given parameters are checked before encryption happens.
Declaration
public ChaCha20(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, uint counter)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<byte> | key | A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit little-endian integers |
ReadOnlySpan<byte> | nonce | A 12-byte (96-bit) nonce, treated as a concatenation of three 32-bit little-endian integers |
uint | counter | A 4-byte (32-bit) block counter, treated as a 32-bit little-endian unsigned integer |
Remarks
See ChaCha20 Spec Section 2.4 for a detailed description of the inputs.
Fields
| Edit this page View SourceallowedKeyLength
Only allowed key lenght in bytes
Declaration
public const int allowedKeyLength = 32
Field Value
Type | Description |
---|---|
int |
allowedNonceLength
Only allowed nonce lenght in bytes
Declaration
public const int allowedNonceLength = 12
Field Value
Type | Description |
---|---|
int |
processBytesAtTime
How many bytes are processed per loop
Declaration
public const int processBytesAtTime = 64
Field Value
Type | Description |
---|---|
int |
Properties
| Edit this page View SourceState
The ChaCha20 state (aka "context"). Read-Only.
Declaration
public uint[] State { get; }
Property Value
Type | Description |
---|---|
uint[] |
Methods
| Edit this page View SourceDecryptBytes(byte[], SimdMode)
Decrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] DecryptBytes(byte[] input, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | input | Input byte array |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
byte[] | Byte array that contains decrypted bytes |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
DecryptBytes(byte[], byte[], SimdMode)
Decrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
Declaration
public void DecryptBytes(byte[] output, byte[] input, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | output | Output byte array, must have enough bytes |
byte[] | input | Input byte array |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
DecryptBytes(byte[], byte[], int, SimdMode)
Decrypt arbitrary-length byte array (input), writing the resulting byte array to the output buffer.
Declaration
public void DecryptBytes(byte[] output, byte[] input, int numBytes, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | output | Output byte array |
byte[] | input | Input byte array |
int | numBytes | Number of bytes to decrypt |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
DecryptBytes(byte[], int, SimdMode)
Decrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] DecryptBytes(byte[] input, int numBytes, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | input | Input byte array |
int | numBytes | Number of bytes to encrypt |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
byte[] | Byte array that contains decrypted bytes |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
DecryptStream(Stream, Stream, int, SimdMode)
Decrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
Declaration
public void DecryptStream(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
Stream | output | Output stream |
Stream | input | Input stream |
int | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
DecryptStreamAsync(Stream, Stream, int, SimdMode)
Async decrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
Declaration
public Task DecryptStreamAsync(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
Stream | output | Output stream |
Stream | input | Input stream |
int | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
Task |
DecryptUTF8ByteArray(byte[], SimdMode)
Decrypt UTF8 byte array to string.
Declaration
public string DecryptUTF8ByteArray(byte[] input, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | input | Byte array |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
string | Byte array that contains encrypted bytes |
Remarks
Here you can NOT swap encrypt and decrypt methods, because of bytes-string transform
Dispose()
Clear and dispose of the internal state. Also request the GC not to call the finalizer, because all cleanup has been taken care of.
Declaration
public void Dispose()
EncryptBytes(byte[], SimdMode)
Encrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] EncryptBytes(byte[] input, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | input | Input byte array |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
byte[] | Byte array that contains encrypted bytes |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
EncryptBytes(byte[], byte[], SimdMode)
Encrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
Declaration
public void EncryptBytes(byte[] output, byte[] input, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | output | Output byte array, must have enough bytes |
byte[] | input | Input byte array |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
EncryptBytes(byte[], byte[], int, SimdMode)
Encrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
Declaration
public void EncryptBytes(byte[] output, byte[] input, int numBytes, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | output | Output byte array, must have enough bytes |
byte[] | input | Input byte array |
int | numBytes | Number of bytes to encrypt |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
EncryptBytes(byte[], int, SimdMode)
Encrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] EncryptBytes(byte[] input, int numBytes, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
byte[] | input | Input byte array |
int | numBytes | Number of bytes to encrypt |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
byte[] | Byte array that contains encrypted bytes |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
EncryptStream(Stream, Stream, int, SimdMode)
Encrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
Declaration
public void EncryptStream(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
Stream | output | Output stream |
Stream | input | Input stream |
int | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
EncryptStreamAsync(Stream, Stream, int, SimdMode)
Async encrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
Declaration
public Task EncryptStreamAsync(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
Stream | output | Output stream |
Stream | input | Input stream |
int | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
Task |
EncryptString(string, SimdMode)
Encrypt string as UTF8 byte array, returns byte array that is allocated by method.
Declaration
public byte[] EncryptString(string input, SimdMode simdMode = SimdMode.AutoDetect)
Parameters
Type | Name | Description |
---|---|---|
string | input | Input string |
SimdMode | simdMode | Chosen SIMD mode (default is auto-detect) |
Returns
Type | Description |
---|---|
byte[] | Byte array that contains encrypted bytes |
Remarks
Here you can NOT swap encrypt and decrypt methods, because of bytes-string transform
~ChaCha20()
Clear and dispose of the internal state. The finalizer is only called if Dispose() was never called on this cipher.
Declaration
protected ~ChaCha20()