Crypt::OpenSSL::Random#
Draw bytes from OpenSSL’s cryptographically secure random-number generator, and seed it.
random_bytes($n) returns $n random bytes, or false when the generator cannot supply them; random_pseudo_bytes($n) is the same (OpenSSL 3 has one generator). random_seed($data) mixes $data into the generator’s state and returns random_status(): 1 when the generator is seeded well enough, which OpenSSL 3 does by itself from the operating system. Nothing is exported by default.
The generator is the one in the system’s libcrypto 3, reached through the built-in Net::SSLeay, so both modules draw from the same state. random_egd is not provided: OpenSSL 3 is built without support for an entropy-gathering daemon, as Random.xs then compiles none.
This is a native implementation of Crypt::OpenSSL::Random 0.17’s Random.xs; its Crypt/OpenSSL/Random.pm is used unchanged.
Synopsis#
use Crypt::OpenSSL::Random qw(random_bytes random_status);
random_status() or die "the generator is not seeded";
my $key = random_bytes(32); # 32 bytes for a key
printf "%d\n", length $key; # 32
Differences from upstream#
Two, each where perl 5.44 with Random.xs fails (t/81-xs-native/Crypt-OpenSSL-Random/150-or-better.t):
A count of 0 returns the empty string. perl5 returns bytes of uninitialised memory, measured with
strlen.A negative count returns false, as a refused request does. perl5 tries to allocate that many bytes and exits “Out of memory”.
Functions#
Random bytes#
random_bytes#
random_bytes($n): $n bytes from the generator, or false when it cannot supply them.
random_pseudo_bytes#
random_pseudo_bytes($n): the same as random_bytes.
Seeding#
random_seed#
random_seed($data): mix the bytes of $data into the generator; returns random_status().
random_status#
random_status(): 1 when the generator is seeded well enough, else 0.