ChaCha20

Purpose

ChaCha20arrow-up-right is an unauthenticated stream cipher that uses a 256-bit key and 96-bit nonce (number used only once) to encrypt/decrypt a message.

The 32-bit internal counter can be changed from the default of 0 to 1 for constructing ChaCha20-Poly1305arrow-up-right. Otherwise, it should generally be left alone.

circle-exclamation
triangle-exclamation

Usage

Fill

Fills a span with pseudorandom bytes computed from a nonce and key. This can be used to compute the Poly1305 key for constructing ChaCha20-Poly1305arrow-up-right.

ChaCha20.Fill(Span<byte> buffer, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> key)

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

buffer has a length of 0.

ArgumentOutOfRangeExceptionarrow-up-right

nonce has a length not equal to NonceSize.

ArgumentOutOfRangeExceptionarrow-up-right

key has a length not equal to KeySize.

CryptographicExceptionarrow-up-right

Error computing pseudorandom bytes.

Encrypt

Fills a span with ciphertext computed from a plaintext message, nonce, and key.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

ciphertext has a length not equal to plaintext.Length.

ArgumentOutOfRangeExceptionarrow-up-right

nonce has a length not equal to NonceSize.

ArgumentOutOfRangeExceptionarrow-up-right

key has a length not equal to KeySize.

CryptographicExceptionarrow-up-right

Encryption failed or counter overflow prevented.

Decrypt

Fills a span with plaintext computed from a ciphertext message, nonce, and key.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

plaintext has a length not equal to ciphertext.Length.

ArgumentOutOfRangeExceptionarrow-up-right

nonce has a length not equal to NonceSize.

ArgumentOutOfRangeExceptionarrow-up-right

key has a length not equal to KeySize.

CryptographicExceptionarrow-up-right

Decryption failed or counter overflow prevented.

Constants

These are used for validation and/or save you defining your own constants.

Notes

triangle-exclamation
triangle-exclamation
circle-exclamation
circle-exclamation

Last updated