.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Crypt::PRNG 3pm" .TH Crypt::PRNG 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Crypt::PRNG \- Cryptographically secure random number generator .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& ### 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(\*(AqACGT\*(Aq, 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(\*(AqACGT\*(Aq, 64); \& $floating_point_number_0_to_1 = $prng\->double; \& $floating_point_number_0_to_88 = $prng\->double(88); \& $unsigned_32bit_int = $prng\->int32; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Provides an interface to the ChaCha20 based pseudo random number generator (thread-safe and fork-safe). .SH FUNCTIONS .IX Header "FUNCTIONS" .SS random_bytes .IX Subsection "random_bytes" .Vb 1 \& $octets = random_bytes($length); .Ve .PP Returns \f(CW$length\fR random octects. .SS random_bytes_hex .IX Subsection "random_bytes_hex" .Vb 1 \& $hex_string = random_bytes_hex($length); .Ve .PP Returns \f(CW$length\fR random octects encoded as hexadecimal string. .SS random_bytes_b64 .IX Subsection "random_bytes_b64" .Vb 1 \& $base64_string = random_bytes_b64($length); .Ve .PP Returns \f(CW$length\fR random octects Base64 encoded. .SS random_bytes_b64u .IX Subsection "random_bytes_b64u" .Vb 1 \& $base64url_string = random_bytes_b64u($length); .Ve .PP Returns \f(CW$length\fR random octects Base64 URL Safe (RFC 4648 section 5) encoded. .SS random_string_from .IX Subsection "random_string_from" .Vb 3 \& $string = random_string_from($range, $length); \& #e.g. \& $string = random_string_from("ABCD", 10); .Ve .PP Returns a random string made of \f(CW$length\fR chars randomly chosen from \f(CW$range\fR string. .SS random_string .IX Subsection "random_string" .Vb 3 \& $alphanumeric_string = random_string($length); \& #or \& $alphanumeric_string = random_string; # default length = 20 .Ve .PP Similar to random_string_from, only \f(CW$range\fR is fixed to \f(CW\*(AqABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\*(Aq\fR. .SS rand .IX Subsection "rand" .Vb 3 \& $n = rand; \& #or \& $n = rand($limit); .Ve .PP Returns a random floating point number from range \f(CW\*(C`[0,1)\*(C'\fR (if called without parameter) or \f(CW\*(C`[0,$limit)\*(C'\fR. .SS irand .IX Subsection "irand" .Vb 1 \& $i = irand; .Ve .PP Returns a random unsigned 32bit integer \- range \f(CW\*(C`0 .. 0xFFFFFFFF\*(C'\fR. .SH METHODS .IX Header "METHODS" .SS new .IX Subsection "new" .Vb 5 \& $prng = Crypt::PRNG\->new; \& #or \& $prng = Crypt::PRNG\->new($alg); \& #or \& $prng = Crypt::PRNG\->new($alg, $seed); \& \& # $alg ... algorithm name \*(AqFrotuna\*(Aq (DEFAULT), \*(AqRC4\*(Aq, \*(AqSober128\*(Aq or \*(AqYarrow\*(Aq \& # $seed ... will be used as an initial entropy for seeding PRNG .Ve .PP If \f(CW$seed\fR is not specified the PRNG is automatically seeded with 32bytes random data taken from \f(CW\*(C`/dev/random\*(C'\fR (UNIX) or \f(CW\*(C`CryptGenRandom\*(C'\fR (Win32) .SS add_entropy .IX Subsection "add_entropy" .Vb 3 \& $prng\->add_entropy($random_data); \& #or \& $prng\->add_entropy(); .Ve .PP If called without parameter it uses 32bytes random data taken from \f(CW\*(C`/dev/random\*(C'\fR (UNIX) or \f(CW\*(C`CryptGenRandom\*(C'\fR (Win32). .PP \&\fBBEWARE:\fR 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. .SS bytes .IX Subsection "bytes" .Vb 1 \& $octets = $prng\->bytes($length); .Ve .PP See random_bytes .SS bytes_hex .IX Subsection "bytes_hex" .Vb 1 \& $hex_string = $prng\->bytes_hex($length); .Ve .PP See random_bytes_hex .SS bytes_b64 .IX Subsection "bytes_b64" .Vb 1 \& $base64_string = $prng\->bytes_b64($length); .Ve .PP See random_bytes_b64 .SS bytes_b64u .IX Subsection "bytes_b64u" .Vb 1 \& $base64url_string = $prng\->bytes_b64u($length); .Ve .PP See random_bytes_b64u .SS string .IX Subsection "string" .Vb 3 \& $alphanumeric_string = $prng\->string($length); \& #or \& $alphanumeric_string = $prng\->string; .Ve .PP See random_string .SS string_from .IX Subsection "string_from" .Vb 1 \& $string = $prng\->string_from($range, $length); .Ve .PP See random_string_from .SS double .IX Subsection "double" .Vb 3 \& $n = $prng\->double; \& #or \& $n = $prng\->double($limit); .Ve .PP See rand .SS int32 .IX Subsection "int32" .Vb 1 \& $i = $prng\->int32; .Ve .PP See irand .SH "SEE ALSO" .IX Header "SEE ALSO" Crypt::PRNG::Fortuna, Crypt::PRNG::RC4, Crypt::PRNG::Sober128, Crypt::PRNG::Yarrow