Crypt::PRNG(3pm)

NAME
Crypt::PRNG - Cryptographically secure random number generator

SYNOPSIS
   ### 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.

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.

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

Returns $length random octets as a binary string.

  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.

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

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

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

  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.

  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.