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:
neworset_iv- optionally
adata_add - zero or more
encrypt_addordecrypt_addcalls - then
encrypt_doneordecrypt_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$noncehere, you gottaset_ivlater. 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 π
- CryptX, Crypt::AuthEnc::XChaCha20Poly1305, Crypt::AuthEnc::GCM, Crypt::AuthEnc::CCM, Crypt::AuthEnc::EAX, Crypt::AuthEnc::OCB
- <https://www.rfc-editor.org/rfc/rfc7539>
β 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
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms