| 1 | package Crypt::PRNG::ChaCha20; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '0.091'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use base qw(Crypt::PRNG Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] ); | |||||||||||||||||||||||||||
| 9 | 3 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 10 | 3 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use CryptX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 4 | my ($class, @args) = @_; | |||||||||||||||||||||||||||
| 16 | 4 | my $obj = Crypt::PRNG->new('ChaCha20', @args); | |||||||||||||||||||||||||||
| 17 | 4 | return bless $obj, $class; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | ### stolen from Bytes::Random::Secure | ||||||||||||||||||||||||||||
| 22 | 3 | my $RNG_object = undef; | |||||||||||||||||||||||||||
| 23 | 3006 | ▶ | my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once. | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 24 | 3006 | ▶ | $RNG_object = Crypt::PRNG::ChaCha20->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $RNG_object and ref $RNG_object ne "SCALAR"
MC/DC50%1/2
defined $RNG_object and ref $RNG_object ne "SCALAR"
defined $RNG_object
ref $RNG_object ne "SCALAR"
Truth table66%2/3
A and B
A= defined $RNG_objectB= ref $RNG_object ne "SCALAR"
| |||||||||||||||||||||||||||||
| 25 | 3006 | return $RNG_object; | |||||||||||||||||||||||||||
| 26 | 3 | }; | |||||||||||||||||||||||||||
| 27 | 2000 | ▶ | sub rand { return $fetch_RNG->()->double(@_) } | ||||||||||||||||||||||||||
|
Subroutine
rand: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 1000 | ▶ | sub irand { return $fetch_RNG->()->int32(@_) } | ||||||||||||||||||||||||||
|
Subroutine
irand: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub random_bytes { return $fetch_RNG->()->bytes(@_) } | ||||||||||||||||||||||||||
|
Subroutine
random_bytes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub random_bytes_hex { return $fetch_RNG->()->bytes_hex(@_) } | ||||||||||||||||||||||||||
|
Subroutine
random_bytes_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub random_bytes_b64 { return $fetch_RNG->()->bytes_b64(@_) } | ||||||||||||||||||||||||||
|
Subroutine
random_bytes_b64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub random_bytes_b64u { return $fetch_RNG->()->bytes_b64u(@_) } | ||||||||||||||||||||||||||
|
Subroutine
random_bytes_b64u: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub random_string_from { return $fetch_RNG->()->string_from(@_) } | ||||||||||||||||||||||||||
|
Subroutine
random_string_from: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub random_string { return $fetch_RNG->()->string(@_) } | ||||||||||||||||||||||||||
|
Subroutine
random_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | 1; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =pod | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Crypt::PRNG::ChaCha20 - Cryptographically secure PRNG based on ChaCha20 (stream cipher) algorithm | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | ### Functional interface: | ||||||||||||||||||||||||||||
| 49 | use Crypt::PRNG::ChaCha20 qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $octets = random_bytes(45); | ||||||||||||||||||||||||||||
| 52 | my $hex_string = random_bytes_hex(45); | ||||||||||||||||||||||||||||
| 53 | my $base64_string = random_bytes_b64(45); | ||||||||||||||||||||||||||||
| 54 | my $base64url_string = random_bytes_b64u(45); | ||||||||||||||||||||||||||||
| 55 | my $alphanumeric_string = random_string(30); | ||||||||||||||||||||||||||||
| 56 | my $string = random_string_from('ACGT', 64); | ||||||||||||||||||||||||||||
| 57 | my $floating_point_number_0_to_1 = rand; | ||||||||||||||||||||||||||||
| 58 | my $floating_point_number_0_to_88 = rand(88); | ||||||||||||||||||||||||||||
| 59 | my $unsigned_32bit_int = irand; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | ### OO interface: | ||||||||||||||||||||||||||||
| 62 | use Crypt::PRNG::ChaCha20; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $prng = Crypt::PRNG::ChaCha20->new; | ||||||||||||||||||||||||||||
| 65 | my $seeded_prng = Crypt::PRNG::ChaCha20->new("some data used for seeding PRNG"); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my $octets = $prng->bytes(45); | ||||||||||||||||||||||||||||
| 68 | my $hex_string = $prng->bytes_hex(45); | ||||||||||||||||||||||||||||
| 69 | my $base64_string = $prng->bytes_b64(45); | ||||||||||||||||||||||||||||
| 70 | my $base64url_string = $prng->bytes_b64u(45); | ||||||||||||||||||||||||||||
| 71 | my $alphanumeric_string = $prng->string(30); | ||||||||||||||||||||||||||||
| 72 | my $string = $prng->string_from('ACGT', 64); | ||||||||||||||||||||||||||||
| 73 | my $floating_point_number_0_to_1 = $prng->double; | ||||||||||||||||||||||||||||
| 74 | my $floating_point_number_0_to_88 = $prng->double(88); | ||||||||||||||||||||||||||||
| 75 | my $unsigned_32bit_int = $prng->int32; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Provides an interface to the ChaCha20-based pseudo-random number generator. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | This is a thin wrapper around L<Crypt::PRNG> with the algorithm fixed to | ||||||||||||||||||||||||||||
| 82 | ChaCha20. All functions and methods accept the same arguments and return the | ||||||||||||||||||||||||||||
| 83 | same values as the corresponding L<Crypt::PRNG> entries. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Nothing is exported by default. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | You can export selected functions: | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | use Crypt::PRNG::ChaCha20 qw(random_bytes random_string); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Or all of them at once: | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | use Crypt::PRNG::ChaCha20 ':all'; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | All functions below behave exactly like the corresponding L<Crypt::PRNG> | ||||||||||||||||||||||||||||
| 100 | functions, but use a hidden C<Crypt::PRNG::ChaCha20> object internally. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 random_bytes | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | See L<Crypt::PRNG/random_bytes>. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 random_bytes_hex | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | See L<Crypt::PRNG/random_bytes_hex>. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 random_bytes_b64 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | See L<Crypt::PRNG/random_bytes_b64>. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 random_bytes_b64u | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | See L<Crypt::PRNG/random_bytes_b64u>. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 random_string | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | See L<Crypt::PRNG/random_string>. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 random_string_from | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | See L<Crypt::PRNG/random_string_from>. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 rand | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | See L<Crypt::PRNG/rand>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 irand | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | See L<Crypt::PRNG/irand>. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 METHODS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Unless noted otherwise, assume C<$prng> is an existing | ||||||||||||||||||||||||||||
| 137 | C<Crypt::PRNG::ChaCha20> object created via C<new>. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 new | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | my $prng = Crypt::PRNG::ChaCha20->new; | ||||||||||||||||||||||||||||
| 142 | my $seeded_prng = Crypt::PRNG::ChaCha20->new($seed); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Creates a PRNG object using the ChaCha20 algorithm. If C<$seed> is omitted, | ||||||||||||||||||||||||||||
| 145 | the object is automatically seeded by the underlying L<Crypt::PRNG> logic. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 bytes | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | See L<Crypt::PRNG/bytes>. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 bytes_hex | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | See L<Crypt::PRNG/bytes_hex>. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 bytes_b64 | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | See L<Crypt::PRNG/bytes_b64>. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 bytes_b64u | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | See L<Crypt::PRNG/bytes_b64u>. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 string | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | See L<Crypt::PRNG/string>. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 string_from | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | See L<Crypt::PRNG/string_from>. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 double | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | See L<Crypt::PRNG/double>. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 int32 | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | See L<Crypt::PRNG/int32>. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =over | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item * L<Crypt::PRNG> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * L<https://www.rfc-editor.org/rfc/rfc7539> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =back | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||