AeadChaCha20Poly1305.NetCore 2.0.0

dotnet add package AeadChaCha20Poly1305.NetCore --version 2.0.0
                    
NuGet\Install-Package AeadChaCha20Poly1305.NetCore -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AeadChaCha20Poly1305.NetCore" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AeadChaCha20Poly1305.NetCore" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AeadChaCha20Poly1305.NetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AeadChaCha20Poly1305.NetCore --version 2.0.0
                    
#r "nuget: AeadChaCha20Poly1305.NetCore, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AeadChaCha20Poly1305.NetCore@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AeadChaCha20Poly1305.NetCore&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AeadChaCha20Poly1305.NetCore&version=2.0.0
                    
Install as a Cake Tool

AeadChaCha20Poly1305.NetCore

License: MIT nuget

AeadChaCha20Poly1305.NetCore is a .NET implementation of AEAD_CHACHA20_POLY1305 (RFC 8439).

It provides authenticated encryption with associated data (AEAD) using:

  • ChaCha20 for encryption/decryption
  • Poly1305 for authentication tags

The API is optimized for integration with PinnedMemory and supports both PinnedMemory<byte> and byte[] output workflows.

You can read more in:


Table of contents


Requirements

  • .NET 8 SDK for building and testing this repository.
  • Target runtime/framework for the package: .NET 8 (net8.0).

The repository includes a global.json to pin the SDK family used for development.


Installation

NuGet Package Manager (CLI)

dotnet add package AeadChaCha20Poly1305.NetCore

Package Manager Console

Install-Package AeadChaCha20Poly1305.NetCore

Quick start

Encrypt and authenticate

using System;
using System.Security.Cryptography;
using AeadChaCha20Poly1305.NetCore;
using PinnedMemory;

var nonce = new byte[12];   // 96-bit nonce per RFC 8439
var keyBytes = new byte[32]; // 256-bit key
var plaintext = new byte[1024];
var associatedData = new byte[] { 0x20, 0x21, 0x22 };

RandomNumberGenerator.Fill(nonce);
RandomNumberGenerator.Fill(keyBytes);
RandomNumberGenerator.Fill(plaintext);

using var key = new PinnedMemory<byte>(keyBytes, false);
using var cipher = new AeadChaCha20Poly1305(key, nonce, associatedData);

cipher.UpdateBlock(plaintext, 0, plaintext.Length);

using var ciphertext = new PinnedMemory<byte>(new byte[cipher.GetLength()]);
cipher.DoFinal(ciphertext, 0);

var tag = cipher.GetTag(); // 16-byte authentication tag

Decrypt and verify

// Reuse the same key, nonce, and associated data from encryption.
// Provide the tag obtained from the encryption phase.

cipher.Reset();
cipher.SetTag(tag!);
cipher.UpdateBlock(ciphertext, 0, ciphertext.Length);

using var decrypted = new PinnedMemory<byte>(new byte[cipher.GetLength()]);
cipher.DoFinal(decrypted, 0);

// If tag verification fails, DoFinal throws ArgumentException.

For additional runnable examples, see AeadChaCha20Poly1305.NetCore.Examples.


API reference

AeadChaCha20Poly1305

Constructor

AeadChaCha20Poly1305(PinnedMemory<byte> key, byte[] nonce, byte[]? ad = null, int rounds = 20)
  • key must be exactly 32 bytes.
  • nonce must be exactly 12 bytes.
  • ad is optional associated data and may be null.
  • rounds must be 20 (RFC 8439 requirement).

Core methods

int GetLength()
byte[] GetBuffer()
int GetTagLength()
PinnedMemory<byte>? GetTag()
void SetTag(PinnedMemory<byte> value)
void Update(byte value)
void UpdateBlock(byte[] value, int offset, int length)
void UpdateBlock(PinnedMemory<byte> value, int offset, int length)
void DoFinal(PinnedMemory<byte> output, int offset)
void DoFinal(byte[] output, int offset)
void Reset()
void Dispose()

Behavior notes

  • GetLength() returns the length of buffered input data (ciphertext or plaintext length).
  • GetTagLength() returns 16 bytes.
  • If no tag is set, DoFinal(...) performs encryption and populates an authentication tag.
  • If a tag is set via SetTag(...), DoFinal(...) performs decryption and verifies the provided tag.
  • On tag verification failure, decryption throws ArgumentException.
  • Reset() clears buffered data and tag state but retains key/nonce/ad configuration.
  • Dispose() zeroes key/nonce and releases owned resources.

Best practices

1) Treat nonces as unique per key

Never reuse the same nonce with the same key across different messages.

2) Keep associated data consistent

The exact same associated data bytes used for encryption must be supplied during decryption.

3) Handle tags carefully

Always store/transmit the full 16-byte tag and set it before decryption.

4) Use using and dispose promptly

Dispose key and cipher instances as soon as they are no longer needed.

5) Validate offsets/lengths in caller code

Ensure destination buffers are sized to GetLength() before calling DoFinal(...).


Development

Build

dotnet build AeadChaCha20Poly1305.NetCore.sln

Test

dotnet test AeadChaCha20Poly1305.NetCore.sln

Security notes

  • This library relies on .NET cryptography primitives (ChaCha20Poly1305) for AEAD operations.
  • Authentication must always be verified before trusting decrypted plaintext.
  • Keep key material secret and prefer memory-safe handling patterns (PinnedMemory, short-lived buffers, disposal).

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 56 2/24/2026
1.0.1 762 8/11/2020
1.0.0 631 8/11/2020