Crypt::PRNG(3) User Contributed Perl Documentation Crypt::PRNG(3)

Crypt::PRNG - Cryptographically secure random number generator

### Functional interface:
use Crypt::PRNG qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u
                   random_string random_string_from rand irand);
$octets = random_bytes(45);
$hex_string = random_bytes_hex(45);
$base64_string = random_bytes_b64(45);
$base64url_string = random_bytes_b64u(45);
$alphanumeric_string = random_string(30);
$string = random_string_from('ACGT', 64);
$floating_point_number_0_to_1 = rand;
$floating_point_number_0_to_88 = rand(88);
$unsigned_32bit_int = irand;
### OO interface:
use Crypt::PRNG;
$prng = Crypt::PRNG->new;
#or
$prng = Crypt::PRNG->new("RC4");
#or
$prng = Crypt::PRNG->new("RC4", "some data used for seeding PRNG");
$octets = $prng->bytes(45);
$hex_string = $prng->bytes_hex(45);
$base64_string = $prng->bytes_b64(45);
$base64url_string = $prng->bytes_b64u(45);
$alphanumeric_string = $prng->string(30);
$string = $prng->string_from('ACGT', 64);
$floating_point_number_0_to_1 = $prng->double;
$floating_point_number_0_to_88 = $prng->double(88);
$unsigned_32bit_int = $prng->int32;

Provides an interface to the ChaCha20 based pseudo random number generator (thread-safe and fork-safe).

$octets = random_bytes($length);

Returns $length random octects.

$hex_string = random_bytes_hex($length);

Returns $length random octects encoded as hexadecimal string.

$base64_string = random_bytes_b64($length);

Returns $length random octects Base64 encoded.

$base64url_string = random_bytes_b64u($length);

Returns $length random octects Base64 URL Safe (RFC 4648 section 5) encoded.

$string = random_string_from($range, $length);
#e.g.
$string = random_string_from("ABCD", 10);

Returns a random string made of $length chars randomly chosen from $range string.

$alphanumeric_string = random_string($length);
#or
$alphanumeric_string = random_string;  # default length = 20

Similar to random_string_from, only $range is fixed to 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.

$n = rand;
#or
$n = rand($limit);

Returns a random floating point number from range "[0,1)" (if called without parameter) or "[0,$limit)".

$i = irand;

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

$prng = Crypt::PRNG->new;
#or
$prng = Crypt::PRNG->new($alg);
#or
$prng = Crypt::PRNG->new($alg, $seed);
# $alg  ... algorithm name 'Frotuna' (DEFAULT), 'RC4', 'Sober128' or 'Yarrow'
# $seed ... will be used as an initial entropy for seeding PRNG

If $seed is not specified the PRNG is automatically seeded with 32bytes random data taken from "/dev/random" (UNIX) or "CryptGenRandom" (Win32)

$prng->add_entropy($random_data);
#or
$prng->add_entropy();

If called without parameter it uses 32bytes random data taken from "/dev/random" (UNIX) or "CryptGenRandom" (Win32).

BEWARE: you probably do not need this function at all as the module does automatic seeding on initialization as well as reseeding after fork and thread creation.

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

See random_bytes

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

See random_bytes_hex

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

See random_bytes_b64

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

See random_bytes_b64u

$alphanumeric_string = $prng->string($length);
#or
$alphanumeric_string = $prng->string;

See random_string

$string = $prng->string_from($range, $length);

See random_string_from

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

See rand

$i = $prng->int32;

See irand

Crypt::PRNG::Fortuna, Crypt::PRNG::RC4, Crypt::PRNG::Sober128, Crypt::PRNG::Yarrow

2023-10-09 perl v5.38.0