Crypt::...Poly1305(3)

okay so bestie, welcome to the manual page for Crypt::AuthEnc::XChaCha20Poly1305 in the User Contributed Perl Documentation section (3) πŸŽ€ the header labels it Crypt::...Poly1305(3) β€” yes it’s that truncated-looking name, iykyk πŸ’…

🌸 NAME 🌸

Crypt::AuthEnc::XChaCha20Poly1305 β€” Authenticated encryption in XChaCha20-Poly1305 mode ✨

✨ SYNOPSIS ✨

okay bestie, here’s the whole thing in both flavors β€” OO interface and functional interface, copy-paste energy, no cap:

### OO interface
use Crypt::AuthEnc::XChaCha20Poly1305;

my $key = '...';
my $nonce = '...';
my $expected_tag = '...';

# encrypt and authenticate
my $ae_enc = Crypt::AuthEnc::XChaCha20Poly1305->new($key, $nonce);
$ae_enc->adata_add('additional_authenticated_data1');
$ae_enc->adata_add('additional_authenticated_data2');
my $ct = $ae_enc->encrypt_add('data1');
$ct .= $ae_enc->encrypt_add('data2');
my $tag = $ae_enc->encrypt_done();

# decrypt and verify
my $ae_dec = Crypt::AuthEnc::XChaCha20Poly1305->new($key, $nonce);
$ae_dec->adata_add('additional_authenticated_data1');
my $pt = $ae_dec->decrypt_add($ct);
die "decrypt failed" unless $ae_dec->decrypt_done($tag);

### functional interface
use Crypt::AuthEnc::XChaCha20Poly1305 qw(
  xchacha20poly1305_encrypt_authenticate
  xchacha20poly1305_decrypt_verify
);

my $key = '...';
my $nonce = '...';
my $adata = '...';
my $plaintext = '...';

my ($ciphertext, $tag) = xchacha20poly1305_encrypt_authenticate($key, $nonce, $adata, $plaintext);
my $decrypted = xchacha20poly1305_decrypt_verify($key, $nonce, $adata, $ciphertext, $tag);

πŸ’… DESCRIPTION πŸ’…

so this module has been around since CryptX-0.089 πŸ¦‹ It provides encryption and authentication based on XChaCha20 + Poly1305, using the extended 192-bit (24-byte) nonce variant of ChaCha20-Poly1305. slay.

This is a stateful API. You build one message by calling, in order:

  1. new or set_iv
  2. optional adata_add
  3. zero or more encrypt_add or decrypt_add calls
  4. then encrypt_done or decrypt_done

Use a fresh object per message β€” literally, one message per object, no recycling vibes. If you construct with new($key) you must call set_iv($nonce) before adding AAD or processing plaintext/ciphertext.

When verifying, decrypt_done($expected_tag) is the safer one-step form; decrypt_done() without arguments only returns the calculated tag. The first encrypt_done / decrypt_done call finalizes the object. After that, further set_iv, adata_add, encrypt_add, decrypt_add, encrypt_done, and decrypt_done calls croak. 🫠

⚠️ warning bestie: once it’s finalized, it’s finalized β€” those later calls croak, periodt.

πŸ¦‹ EXPORT πŸ¦‹

Nothing is exported by default.

You can export selected functions:

use Crypt::AuthEnc::XChaCha20Poly1305 qw(
  xchacha20poly1305_encrypt_authenticate
  xchacha20poly1305_decrypt_verify
);

πŸ‘‘ FUNCTIONS πŸ‘‘

✨ xchacha20poly1305_encrypt_authenticate ✨

my ($ciphertext, $tag) = xchacha20poly1305_encrypt_authenticate($key, $nonce, $adata, $plaintext);

# $key ..... [binary string] encryption key (256 bits / 32 bytes)
# $nonce ... [binary string] extended nonce (192 bits / 24 bytes)
# $adata ... [binary string] additional authenticated data (optional)

Invalid key or nonce lengths croak. String-overloaded objects are accepted for $key and $nonce. 🫢

🌈 xchacha20poly1305_decrypt_verify 🌈

my $plaintext = xchacha20poly1305_decrypt_verify($key, $nonce, $adata, $ciphertext, $tag);
# on error returns undef

Invalid key or nonce lengths croak. String-overloaded objects are accepted for $key and $nonce.

πŸŽ€ METHODS πŸŽ€

Unless noted otherwise, assume $ae is an existing AEAD object created via new, for example:

my $ae = Crypt::AuthEnc::XChaCha20Poly1305->new($key, $nonce);

🧚 new 🧚

two ways to make one, bestie:

my $ae = Crypt::AuthEnc::XChaCha20Poly1305->new($key, $nonce);
my $ae = Crypt::AuthEnc::XChaCha20Poly1305->new($key);

# $key ..... [binary string] encryption key (256 bits / 32 bytes)
# $nonce ... [binary string] extended nonce (192 bits / 24 bytes)

🌸 adata_add 🌸

Add additional authenticated data. Can be called only before the first encrypt_add or decrypt_add. Returns the object itself (for chaining). ✨ chaining is main character energy.

$ae->adata_add($aad_data);                     # can be called multiple times

πŸ’œ encrypt_add πŸ’œ

Returns a binary string of ciphertext (raw bytes).

my $ciphertext = $ae->encrypt_add($data);      # can be called multiple times

πŸ”₯ encrypt_done πŸ”₯

Returns the authentication tag as a binary string (raw bytes). This call finalizes the current message.

my $tag = $ae->encrypt_done();                 # returns $tag value

πŸ“ decrypt_add πŸ“

Returns a binary string of plaintext (raw bytes).

my $plaintext = $ae->decrypt_add($ciphertext); # can be called multiple times

πŸ’€ decrypt_done πŸ’€

Without argument returns the computed tag as a binary string. With $tag argument returns 1 (success) or 0 (failure). This call finalizes the current message.

my $tag = $ae->decrypt_done;           # returns $tag value
#or
my $result = $ae->decrypt_done($tag);  # returns 1 (success) or 0 (failure)

🌟 set_iv 🌟

my $ae = Crypt::AuthEnc::XChaCha20Poly1305->new($key)->set_iv($nonce);
# $nonce ... [binary string] extended nonce (192 bits / 24 bytes)

Call set_iv before the first adata_add, encrypt_add, or decrypt_add for a message.

πŸ¦‹ clone πŸ¦‹

Returns a copy of the AEAD object in its current state.

my $ae_new = $ae->clone;

🌈 SEE ALSO 🌈

okay bye bestie πŸ’– this page is from perl v5.42.0, dated 2026-05-11, and the footer labels it Crypt::...Poly1305(3). periodt. πŸ’…

πŸ“¦ where this page comes from