Crypt::PRNG(3)

πŸ’– NAME πŸ’–

Crypt::PRNG β€” Cryptographically secure random number generator πŸŽ€

Okay bestie, welcome to the Crypt::PRNG(3) page β€” yes, it's a User Contributed Perl Documentation moment. This module is giving secure-random main character energy, and the default algorithm is ChaCha20. πŸ’…


✨ SYNOPSIS ✨

Here's the group-chat screenshot of how you actually call it πŸ‘‡

### Functional interface:
use Crypt::PRNG qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u
                   random_string random_string_from rand irand);

my $octets = random_bytes(45);
my $hex_string = random_bytes_hex(45);
my $base64_string = random_bytes_b64(45);
my $base64url_string = random_bytes_b64u(45);
my $alphanumeric_string = random_string(30);
my $string = random_string_from('ACGT', 64);
my $floating_point_number_0_to_1 = rand;
my $floating_point_number_0_to_88 = rand(88);
my $unsigned_32bit_int = irand;

### OO interface:
use Crypt::PRNG;

my $prng = Crypt::PRNG->new;  # defaults to ChaCha20
my $rc4_prng = Crypt::PRNG->new("RC4");
my $seeded_prng = Crypt::PRNG->new("RC4", "some data used for seeding PRNG");

my $octets = $prng->bytes(45);
my $hex_string = $prng->bytes_hex(45);
my $base64_string = $prng->bytes_b64(45);
my $base64url_string = $prng->bytes_b64u(45);
my $alphanumeric_string = $prng->string(30);
my $string = $prng->string_from('ACGT', 64);
my $floating_point_number_0_to_1 = $prng->double;
my $floating_point_number_0_to_88 = $prng->double(88);
my $unsigned_32bit_int = $prng->int32;

🌸 DESCRIPTION 🌸

Provides an interface to several pseudo-random number generators (thread-safe and fork-safe). The default algorithm is ChaCha20. ✨

Like, it's thread-safe and fork-safe, which is lowkey iconic. No cap.


πŸŽ€ EXPORT πŸŽ€

Nothing is exported by default. πŸ™…β€β™€οΈ

You can export selected functions:

use Crypt::PRNG qw(random_bytes random_string);

Or all of them at once:

use Crypt::PRNG ':all';

πŸ”₯ FUNCTIONS πŸ”₯

For all random_bytes* functions and the corresponding bytes* methods, $length must not be greater than 1000000000. That's a big number, but respect the limit bestie. ⚠️

random_bytes

my $octets = random_bytes($length);
# $length .. [integer] number of random bytes to generate

Returns $length random octets as a binary string. Slay.

random_bytes_hex

my $hex_string = random_bytes_hex($length);
# $length .. [integer] number of random bytes (output string will be 2x longer)

Returns $length random octets encoded as a lowercase hexadecimal string. The output string is 2x longer β€” hex is extra like that. πŸ’…

random_bytes_b64

my $base64_string = random_bytes_b64($length);
# $length .. [integer] number of random bytes to encode

Returns $length random octets encoded as a Base64 string.

random_bytes_b64u

my $base64url_string = random_bytes_b64u($length);
# $length .. [integer] number of random bytes to encode

Returns $length random octets encoded as a Base64 URL-safe string (RFC 4648 section 5). Very URL-safe, very demure. πŸ¦‹

random_string_from

my $string = random_string_from($range, $length);
# $range  .. [string] alphabet of allowed characters
# $length .. [integer] optional, number of characters (DEFAULT: 20)
#e.g.
my $dna_string = random_string_from("ABCD", 10);

Returns a random string of $length characters chosen from $range. The alphabet must contain between 1 and 65536 characters; longer alphabets return "undef". Yes, undef β€” it said what it said. 😭

random_string

my $alphanumeric_string = random_string($length);
# $length .. [integer] optional, number of characters (DEFAULT: 20)
#or
my $default_alphanumeric_string = random_string;  # default length = 20

Like random_string_from, but $range is fixed to 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.

rand

my $n = rand;
#or
my $limited_n = rand($limit);
# $limit .. [number] optional, upper bound (exclusive)

Returns a random floating-point number in the range [0,1) if called without an argument, or [0,$limit) if $limit is given. If $limit is 0, this behaves as if no limit was given and returns a value in [0,1), matching Perl's built-in rand. Periodt.

irand

my $i = irand;

Returns a random unsigned 32-bit integer in the range 0 .. 0xFFFFFFFF.


πŸ‘‘ METHODS πŸ‘‘

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

my $prng = Crypt::PRNG->new;

new

my $prng = Crypt::PRNG->new;  # defaults to ChaCha20
#or
my $prng = Crypt::PRNG->new($alg);
#or
my $prng = Crypt::PRNG->new($alg, $seed);

# $alg  ... [string] algorithm name: 'ChaCha20' (DEFAULT), 'Fortuna', 'RC4' (legacy; compatibility only), 'Sober128' or 'Yarrow'
# $seed ... [binary string] optional, initial entropy for seeding the PRNG

If $seed is not given, the PRNG is automatically seeded with 40 bytes obtained via libtomcrypt's rng_get_bytes() platform RNG logic.

If $seed is specified it must be non-empty for all algorithms. RC4 is provided for legacy compatibility only, is not recommended for new designs, and requires a seed of at least 5 bytes. So like, RC4 is giving vintage, not fresh. πŸ’€

add_entropy

my $prng = Crypt::PRNG->new;
$prng->add_entropy($random_data);
#or
$prng->add_entropy();

If called without parameter it uses 40 bytes obtained via libtomcrypt's rng_get_bytes() platform RNG logic.

Note: You probably do not need this function. The module seeds itself on initialization and reseeds after fork and thread creation.

bytes

my $octets = $prng->bytes($length);

See random_bytes.

bytes_hex

my $hex_string = $prng->bytes_hex($length);

See random_bytes_hex.

bytes_b64

my $base64_string = $prng->bytes_b64($length);

See random_bytes_b64.

bytes_b64u

my $base64url_string = $prng->bytes_b64u($length);

See random_bytes_b64u.

string

my $alphanumeric_string = $prng->string($length);
#or
my $default_alphanumeric_string = $prng->string;  # default length = 20

See random_string.

string_from

my $string = $prng->string_from($range, $length);  # default length = 20

See random_string_from.

double

my $n = $prng->double;
#or
my $limited_n = $prng->double($limit);

See rand.

int32

my $i = $prng->int32;

See irand.


🌈 SEE ALSO 🌈

  • Crypt::PRNG::ChaCha20
  • Crypt::PRNG::Fortuna
  • Crypt::PRNG::RC4
  • Crypt::PRNG::Sober128
  • Crypt::PRNG::Yarrow

For generating random UUIDs see "random_v4uuid" in Crypt::Misc and "random_v7uuid" in Crypt::Misc.


That's the whole vibe check, bestie. This page is from perl v5.42.0, dated 2026-05-11, and it's the Crypt::PRNG(3) manual. Stay random, stay secure πŸ’–πŸ¦‹πŸŽ€

πŸ“¦ where this page comes from