Crypt::...oly1305(3)

πŸ’– NAME πŸ’–

Crypt::AuthEnc::ChaCha20Poly1305 β€” authenticated encryption in ChaCha20-Poly1305 mode, bestie πŸ’…


✨ SYNOPSIS ✨

okay so first up, the OO interface, and yes it goes hard πŸ‘‘

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

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

# encrypt and authenticate
my $ae_enc = Crypt::AuthEnc::ChaCha20Poly1305->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');
$ct .= $ae_enc->encrypt_add('data3');
my $tag = $ae_enc->encrypt_done();

# decrypt and verify
my $ae_dec = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $nonce);
$ae_dec->adata_add('additional_authenticated_data1');
$ae_dec->adata_add('additional_authenticated_data2');
my $pt = $ae_dec->decrypt_add('ciphertext1');
$pt .= $ae_dec->decrypt_add('ciphertext2');
$pt .= $ae_dec->decrypt_add('ciphertext3');
my $computed_tag = $ae_dec->decrypt_done();
die "decrypt failed" unless $computed_tag eq $expected_tag;

#or
my $result = $ae_dec->decrypt_done($expected_tag); # 0 or 1

and then the functional interface, because sometimes you just want a one-liner moment πŸ¦‹

### functional interface
use Crypt::AuthEnc::ChaCha20Poly1305 qw(chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify);

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

my ($ciphertext, $tag) = chacha20poly1305_encrypt_authenticate($key, $nonce, $adata, $plaintext);
my $decrypted = chacha20poly1305_decrypt_verify($key, $nonce, $adata, $ciphertext, $tag);

🌸 DESCRIPTION 🌸

so this module gives you authenticated encryption in ChaCha20-Poly1305 mode, exactly as defined in RFC 7539 <https://www.rfc-editor.org/rfc/rfc7539>. no cap, it's the real spec πŸ’œ

The vibe check: this here is a stateful API. You build up one whole message by calling things in order:

  1. new or set_iv
  2. optionally adata_add
  3. zero or more encrypt_add or decrypt_add calls
  4. then encrypt_done or decrypt_done
fr fr important: use a fresh object per message. no reusing, that's not the move πŸ™ˆ

If you construct with new($key) then you must call set_iv($iv) before adding AAD or processing plaintext/ciphertext. like, mandatory bestie.

When you're verifying, decrypt_done($expected_tag) is the safer one-step form πŸ›‘οΈ β€” decrypt_done() with no arguments only returns the calculated tag, which means you gotta do the comparing yourself.

The first encrypt_done / decrypt_done call finalizes the object. After that it's done, it's over, the era has ended β€” any further set_iv, set_iv_rfc7905, adata_add, encrypt_add, decrypt_add, encrypt_done, and decrypt_done calls will croak πŸ˜­πŸ’€


πŸŽ€ EXPORT πŸŽ€

Nothing is exported by default. (we keep it lowkey, not clingy)

You can export selected functions though, if that's your thing:

use Crypt::AuthEnc::ChaCha20Poly1305 qw(chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify);

πŸ”₯ FUNCTIONS πŸ”₯

chacha20poly1305_encrypt_authenticate

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

# $key ..... [binary string] key of proper length (128 or 256 bits / 16 or 32 bytes)
# $nonce ... [binary string] nonce (64 or 96 bits / 8 or 12 bytes)
# $adata ... [binary string] additional authenticated data (optional)

chacha20poly1305_decrypt_verify

my $plaintext = chacha20poly1305_decrypt_verify($key, $nonce, $adata, $ciphertext, $tag);
# on error returns undef
heads up πŸ‘€ on error this returns undef β€” so check it, bestie, don't just vibe forward and assume it worked.

πŸ’… METHODS πŸ’…

unless noted otherwise, just assume $ae is an existing AEAD object you made via new, like so:

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

new

make the object, it's giving main character energy ✨

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

# $key ..... [binary string] encryption key of proper length (128 or 256 bits / 16 or 32 bytes)
# $nonce ... [binary string] nonce (64 or 96 bits / 8 or 12 bytes)
remember bestie: if you skip the $nonce here, you gotta set_iv later. don't forget. i'm watching 🫢

adata_add

Adds additional authenticated data. You can only call this before the first encrypt_add or decrypt_add. Returns the object itself, so you can chain it like the icon you are πŸ’…

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

encrypt_add

Returns a binary string of ciphertext (raw bytes, no encoding nonsense here):

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. periodt.

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

No argument? you get the computed tag back as a binary string. Pass the $tag argument? you get 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)
iykyk: the version with $tag is the glow-up β€” it does the comparison for you πŸ“

set_iv

my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key)->set_iv($nonce);
# $nonce ... [binary string] nonce (64 or 96 bits / 8 or 12 bytes)

Call set_iv before the first adata_add, encrypt_add, or decrypt_add for a message. that's the order, bestie 🧚


set_iv_rfc7905

See RFC 7905 <https://www.rfc-editor.org/rfc/rfc7905>.

my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key)->set_iv_rfc7905($nonce, $seqnum);
# $nonce ... [binary string] nonce (96 bits / 12 bytes)
# $seqnum .. [integer] 64-bit integer (sequence number)

clone

Returns a copy of the AEAD object in its current state. twin energy πŸ‘―β€β™€οΈ

my $ae_new = $ae->clone;

🌈 SEE ALSO 🌈


⭐ okay that's the whole page, bestie β€” this one's brought to you by perl v5.42.0, dated 2026-05-11, and it's giving User Contributed Perl Documentation the way it always does πŸ’– go encrypt something iconic πŸ‘‘

πŸ“¦ where this page comes from