Crypt::Stream::XSalsa20(3) User Contributed Perl Documentation Crypt::Stream::XSalsa20(3)

Crypt::Stream::XSalsa20 - Stream cipher XSalsa20

use Crypt::Stream::XSalsa20;
# encrypt
my $key    = "12345678901234567890123456789012";  # 32 bytes
my $nonce  = "123456789012345678901234";          # 24 bytes
my $enc_stream = Crypt::Stream::XSalsa20->new($key, $nonce);
my $ct = $enc_stream->crypt("plain message");
# decrypt
my $dec_stream = Crypt::Stream::XSalsa20->new($key, $nonce);
my $pt = $dec_stream->crypt($ct);

Since: CryptX-0.089

Provides an interface to the XSalsa20 stream cipher, an extended-nonce variant of Salsa20 with a 192-bit (24-byte) nonce. The larger nonce makes random nonce generation safe in practice.

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

my $stream = Crypt::Stream::XSalsa20->new($key, $nonce);

my $stream = Crypt::Stream::XSalsa20->new($key, $nonce);
#or
my $stream = Crypt::Stream::XSalsa20->new($key, $nonce, $rounds);
# $key    .. [binary string] 32 bytes
# $nonce  .. [binary string] 24 bytes
# $rounds .. [integer] optional, rounds (DEFAULT: 20)

Encrypts or decrypts data. The output has the same length as the input. Returns a binary string (raw bytes).

The input is converted using Perl's usual scalar stringification. Passing "undef" is treated as an empty string with the usual warning, and numeric scalars are stringified before processing.

my $ciphertext = $stream->crypt($plaintext);
#or
my $plaintext = $stream->crypt($ciphertext);

Returns $length bytes of raw keystream as a binary string.

The length is taken using Perl's usual numeric coercion. Values that coerce to an oversized unsigned length are rejected as too large.

my $random_bytes = $stream->keystream($length);

Returns a copy of the stream cipher object in its current state.

my $stream2 = $stream->clone;

2026-05-12 perl v5.42.2