Class ChaCha20
Class that can be used for ChaCha20 encryption / decryption
Inheritance
Implements
Inherited Members
Namespace: CSChaCha20
Assembly: cs.temp.dll.dll
Syntax
public sealed class ChaCha20 : IDisposable
Constructors
ChaCha20(Byte[], Byte[], UInt32)
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 |
---|---|---|
System.Byte[] | key | A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit little-endian integers |
System.Byte[] | nonce | A 12-byte (96-bit) nonce, treated as a concatenation of three 32-bit little-endian integers |
System.UInt32 | 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.
Fields
allowedKeyLength
Only allowed key lenght in bytes
Declaration
public const int allowedKeyLength = 32
Field Value
Type | Description |
---|---|
System.Int32 |
allowedNonceLength
Only allowed nonce lenght in bytes
Declaration
public const int allowedNonceLength = 12
Field Value
Type | Description |
---|---|
System.Int32 |
processBytesAtTime
How many bytes are processed per loop
Declaration
public const int processBytesAtTime = 64
Field Value
Type | Description |
---|---|
System.Int32 |
Properties
State
The ChaCha20 state (aka "context"). Read-Only.
Declaration
public uint[] State { get; }
Property Value
Type | Description |
---|---|
System.UInt32[] |
Methods
DecryptBytes(Byte[])
Decrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] DecryptBytes(byte[] input)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | input | Input byte array |
Returns
Type | Description |
---|---|
System.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[])
Decrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
Declaration
public void DecryptBytes(byte[] output, byte[] input)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | output | Output byte array, must have enough bytes |
System.Byte[] | input | Input byte array |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
DecryptBytes(Byte[], Byte[], Int32)
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)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | output | Output byte array |
System.Byte[] | input | Input byte array |
System.Int32 | numBytes | Number of bytes to decrypt |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
DecryptBytes(Byte[], Int32)
Decrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] DecryptBytes(byte[] input, int numBytes)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | input | Input byte array |
System.Int32 | numBytes | Number of bytes to encrypt |
Returns
Type | Description |
---|---|
System.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, Int32)
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)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | Output stream |
System.IO.Stream | input | Input stream |
System.Int32 | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
DecryptStreamAsync(Stream, Stream, Int32)
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)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | Output stream |
System.IO.Stream | input | Input stream |
System.Int32 | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
DecryptUTF8ByteArray(Byte[])
Decrypt UTF8 byte array to string.
Declaration
public string DecryptUTF8ByteArray(byte[] input)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | input | Byte array |
Returns
Type | Description |
---|---|
System.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[])
Encrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] EncryptBytes(byte[] input)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | input | Input byte array |
Returns
Type | Description |
---|---|
System.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[])
Encrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
Declaration
public void EncryptBytes(byte[] output, byte[] input)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | output | Output byte array, must have enough bytes |
System.Byte[] | input | Input byte array |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
EncryptBytes(Byte[], Byte[], Int32)
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)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | output | Output byte array, must have enough bytes |
System.Byte[] | input | Input byte array |
System.Int32 | numBytes | Number of bytes to encrypt |
Remarks
Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method
EncryptBytes(Byte[], Int32)
Encrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
Declaration
public byte[] EncryptBytes(byte[] input, int numBytes)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | input | Input byte array |
System.Int32 | numBytes | Number of bytes to encrypt |
Returns
Type | Description |
---|---|
System.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, Int32)
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)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | Output stream |
System.IO.Stream | input | Input stream |
System.Int32 | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
EncryptStreamAsync(Stream, Stream, Int32)
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)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | output | Output stream |
System.IO.Stream | input | Input stream |
System.Int32 | howManyBytesToProcessAtTime | How many bytes to read and write at time, default is 1024 |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
EncryptString(String)
Encrypt string as UTF8 byte array, returns byte array that is allocated by method.
Declaration
public byte[] EncryptString(string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | Input string |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array that contains encrypted bytes |
Remarks
Here you can NOT swap encrypt and decrypt methods, because of bytes-string transform
Finalize()
Clear and dispose of the internal state. The finalizer is only called if Dispose() was never called on this cipher.
Declaration
protected void Finalize()