• Api Documentation
Show / Hide Table of Contents
  • CSChaCha20
    • ChaCha20
    • SimdMode
    • Util

Class ChaCha20

Class for ChaCha20 encryption / decryption

Inheritance
object
ChaCha20
Implements
IDisposable
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: CSChaCha20
Assembly: .dll
Syntax
public sealed class ChaCha20 : IDisposable

Constructors

| Edit this page View Source

ChaCha20(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.

| Edit this page View Source

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 Source

allowedKeyLength

Only allowed key lenght in bytes

Declaration
public const int allowedKeyLength = 32
Field Value
Type Description
int
| Edit this page View Source

allowedNonceLength

Only allowed nonce lenght in bytes

Declaration
public const int allowedNonceLength = 12
Field Value
Type Description
int
| Edit this page View Source

processBytesAtTime

How many bytes are processed per loop

Declaration
public const int processBytesAtTime = 64
Field Value
Type Description
int

Properties

| Edit this page View Source

State

The ChaCha20 state (aka "context"). Read-Only.

Declaration
public uint[] State { get; }
Property Value
Type Description
uint[]

Methods

| Edit this page View Source

DecryptBytes(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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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)

| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

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()
| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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)

| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

~ChaCha20()

Clear and dispose of the internal state. The finalizer is only called if Dispose() was never called on this cipher.

Declaration
protected ~ChaCha20()

Implements

IDisposable
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX