1Crypt::PRNG(3) User Contributed Perl Documentation Crypt::PRNG(3)
2
3
4
6 Crypt::PRNG - Cryptographically secure random number generator
7
9 ### Functional interface:
10 use Crypt::PRNG qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u
11 random_string random_string_from rand irand);
12
13 $octets = random_bytes(45);
14 $hex_string = random_bytes_hex(45);
15 $base64_string = random_bytes_b64(45);
16 $base64url_string = random_bytes_b64u(45);
17 $alphanumeric_string = random_string(30);
18 $string = random_string_from('ACGT', 64);
19 $floating_point_number_0_to_1 = rand;
20 $floating_point_number_0_to_88 = rand(88);
21 $unsigned_32bit_int = irand;
22
23 ### OO interface:
24 use Crypt::PRNG;
25
26 $prng = Crypt::PRNG->new;
27 #or
28 $prng = Crypt::PRNG->new("RC4");
29 #or
30 $prng = Crypt::PRNG->new("RC4", "some data used for seeding PRNG");
31
32 $octets = $prng->bytes(45);
33 $hex_string = $prng->bytes_hex(45);
34 $base64_string = $prng->bytes_b64(45);
35 $base64url_string = $prng->bytes_b64u(45);
36 $alphanumeric_string = $prng->string(30);
37 $string = $prng->string_from('ACGT', 64);
38 $floating_point_number_0_to_1 = $prng->double;
39 $floating_point_number_0_to_88 = $prng->double(88);
40 $unsigned_32bit_int = $prng->int32;
41
43 Provides an interface to the ChaCha20 based pseudo random number
44 generator (thread-safe and fork-safe).
45
47 random_bytes
48 $octets = random_bytes($length);
49
50 Returns $length random octects.
51
52 random_bytes_hex
53 $hex_string = random_bytes_hex($length);
54
55 Returns $length random octects encoded as hexadecimal string.
56
57 random_bytes_b64
58 $base64_string = random_bytes_b64($length);
59
60 Returns $length random octects Base64 encoded.
61
62 random_bytes_b64u
63 $base64url_string = random_bytes_b64u($length);
64
65 Returns $length random octects Base64 URL Safe (RFC 4648 section 5)
66 encoded.
67
68 random_string_from
69 $string = random_string_from($range, $length);
70 #e.g.
71 $string = random_string_from("ABCD", 10);
72
73 Returns a random string made of $length chars randomly chosen from
74 $range string.
75
76 random_string
77 $alphanumeric_string = random_string($length);
78 #or
79 $alphanumeric_string = random_string; # default length = 20
80
81 Similar to random_string_from, only $range is fixed to
82 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.
83
84 rand
85 $n = rand;
86 #or
87 $n = rand($limit);
88
89 Returns a random floating point number from range "[0,1)" (if called
90 without parameter) or "[0,$limit)".
91
92 irand
93 $i = irand;
94
95 Returns a random unsigned 32bit integer - range "0 .. 0xFFFFFFFF".
96
98 new
99 $prng = Crypt::PRNG->new;
100 #or
101 $prng = Crypt::PRNG->new($alg);
102 #or
103 $prng = Crypt::PRNG->new($alg, $seed);
104
105 # $alg ... algorithm name 'Frotuna' (DEFAULT), 'RC4', 'Sober128' or 'Yarrow'
106 # $seed ... will be used as an initial entropy for seeding PRNG
107
108 If $seed is not specified the PRNG is automatically seeded with 32bytes
109 random data taken from "/dev/random" (UNIX) or "CryptGenRandom" (Win32)
110
111 add_entropy
112 $prng->add_entropy($random_data);
113 #or
114 $prng->add_entropy();
115
116 If called without parameter it uses 32bytes random data taken from
117 "/dev/random" (UNIX) or "CryptGenRandom" (Win32).
118
119 BEWARE: you probably do not need this function at all as the module
120 does automatic seeding on initialization as well as reseeding after
121 fork and thread creation.
122
123 bytes
124 $octets = $prng->bytes($length);
125
126 See random_bytes
127
128 bytes_hex
129 $hex_string = $prng->bytes_hex($length);
130
131 See random_bytes_hex
132
133 bytes_b64
134 $base64_string = $prng->bytes_b64($length);
135
136 See random_bytes_b64
137
138 bytes_b64u
139 $base64url_string = $prng->bytes_b64u($length);
140
141 See random_bytes_b64u
142
143 string
144 $alphanumeric_string = $prng->string($length);
145 #or
146 $alphanumeric_string = $prng->string;
147
148 See random_string
149
150 string_from
151 $string = $prng->string_from($range, $length);
152
153 See random_string_from
154
155 double
156 $n = $prng->double;
157 #or
158 $n = $prng->double($limit);
159
160 See rand
161
162 int32
163 $i = $prng->int32;
164
165 See irand
166
168 Crypt::PRNG::Fortuna, Crypt::PRNG::RC4, Crypt::PRNG::Sober128,
169 Crypt::PRNG::Yarrow
170
171
172
173perl v5.38.0 2023-10-04 Crypt::PRNG(3)