line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::PRNG::ChaCha20; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
53284
|
use strict; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
60
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
70
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.080'; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
10
|
use base qw(Crypt::PRNG Exporter); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
541
|
|
8
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] ); |
9
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
10
|
|
|
|
|
|
|
our @EXPORT = qw(); |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
11
|
use CryptX; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
487
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
### stolen from Bytes::Random::Secure |
16
|
|
|
|
|
|
|
my $RNG_object = undef; |
17
|
|
|
|
|
|
|
my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once. |
18
|
|
|
|
|
|
|
$RNG_object = Crypt::PRNG::ChaCha20->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR'; |
19
|
|
|
|
|
|
|
return $RNG_object; |
20
|
|
|
|
|
|
|
}; |
21
|
2000
|
|
|
2000
|
1
|
6214
|
sub rand { return $fetch_RNG->()->double(@_) } |
22
|
1000
|
|
|
1000
|
1
|
1946
|
sub irand { return $fetch_RNG->()->int32(@_) } |
23
|
1
|
|
|
1
|
1
|
6
|
sub random_bytes { return $fetch_RNG->()->bytes(@_) } |
24
|
1
|
|
|
1
|
1
|
4
|
sub random_bytes_hex { return $fetch_RNG->()->bytes_hex(@_) } |
25
|
1
|
|
|
1
|
1
|
3
|
sub random_bytes_b64 { return $fetch_RNG->()->bytes_b64(@_) } |
26
|
1
|
|
|
1
|
1
|
3
|
sub random_bytes_b64u { return $fetch_RNG->()->bytes_b64u(@_) } |
27
|
1
|
|
|
1
|
1
|
3
|
sub random_string_from { return $fetch_RNG->()->string_from(@_) } |
28
|
1
|
|
|
1
|
1
|
4
|
sub random_string { return $fetch_RNG->()->string(@_) } |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
1; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=pod |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 NAME |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Crypt::PRNG::ChaCha20 - Cryptographically secure PRNG based on ChaCha20 (stream cipher) algorithm |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 SYNOPSIS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
### Functional interface: |
43
|
|
|
|
|
|
|
use Crypt::PRNG::ChaCha20 qw(random_bytes random_bytes_hex random_bytes_b64 random_string random_string_from rand irand); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$octets = random_bytes(45); |
46
|
|
|
|
|
|
|
$hex_string = random_bytes_hex(45); |
47
|
|
|
|
|
|
|
$base64_string = random_bytes_b64(45); |
48
|
|
|
|
|
|
|
$base64url_string = random_bytes_b64u(45); |
49
|
|
|
|
|
|
|
$alphanumeric_string = random_string(30); |
50
|
|
|
|
|
|
|
$string = random_string_from('ACGT', 64); |
51
|
|
|
|
|
|
|
$floating_point_number_0_to_1 = rand; |
52
|
|
|
|
|
|
|
$floating_point_number_0_to_88 = rand(88); |
53
|
|
|
|
|
|
|
$unsigned_32bit_int = irand; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
### OO interface: |
56
|
|
|
|
|
|
|
use Crypt::PRNG::ChaCha20; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$prng = Crypt::PRNG::ChaCha20->new; |
59
|
|
|
|
|
|
|
#or |
60
|
|
|
|
|
|
|
$prng = Crypt::PRNG::ChaCha20->new("some data used for seeding PRNG"); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$octets = $prng->bytes(45); |
63
|
|
|
|
|
|
|
$hex_string = $prng->bytes_hex(45); |
64
|
|
|
|
|
|
|
$base64_string = $prng->bytes_b64(45); |
65
|
|
|
|
|
|
|
$base64url_string = $prng->bytes_b64u(45); |
66
|
|
|
|
|
|
|
$alphanumeric_string = $prng->string(30); |
67
|
|
|
|
|
|
|
$string = $prng->string_from('ACGT', 64); |
68
|
|
|
|
|
|
|
$floating_point_number_0_to_1 = rand; |
69
|
|
|
|
|
|
|
$floating_point_number_0_to_88 = rand(88); |
70
|
|
|
|
|
|
|
$unsigned_32bit_int = irand; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 DESCRIPTION |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Provides an interface to the ChaCha20 based pseudo random number generator |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
All methods and functions are the same as for L. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 FUNCTIONS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 random_bytes |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
See L. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 random_bytes_hex |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
See L. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 random_bytes_b64 |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
See L. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 random_bytes_b64u |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
See L. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 random_string |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
See L. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 random_string_from |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
See L. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 rand |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
See L. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 irand |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
See L. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 METHODS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 new |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
See L. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 bytes |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
See L. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 bytes_hex |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
See L. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 bytes_b64 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
See L. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 bytes_b64u |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
See L. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 string |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
See L. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 string_from |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
See L. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 double |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
See L. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 int32 |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
See L. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SEE ALSO |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * L |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=back |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |