File Coverage

blib/lib/Crypt/PRNG/Sober128.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod 9 9 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Crypt::PRNG::Sober128;
2              
3 3     3   121755 use strict;
  3         7  
  3         97  
4 3     3   19 use warnings;
  3         4  
  3         181  
5             our $VERSION = '0.089';
6              
7 3     3   17 use base qw(Crypt::PRNG Exporter);
  3         6  
  3         1048  
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 3     3   18 use CryptX;
  3         11  
  3         1093  
13              
14             sub new {
15 5     5 1 197343 my ($class, @args) = @_;
16 5         363 my $obj = Crypt::PRNG->new('Sober128', @args);
17 5         38 return bless $obj, $class;
18             }
19              
20             {
21             ### stolen from Bytes::Random::Secure
22             my $RNG_object = undef;
23             my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once.
24             $RNG_object = Crypt::PRNG::Sober128->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
25             return $RNG_object;
26             };
27 2000     2000 1 8745 sub rand { return $fetch_RNG->()->double(@_) }
28 1000     1000 1 2755 sub irand { return $fetch_RNG->()->int32(@_) }
29 1     1 1 5 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
30 1     1 1 4 sub random_bytes_hex { return $fetch_RNG->()->bytes_hex(@_) }
31 1     1 1 4 sub random_bytes_b64 { return $fetch_RNG->()->bytes_b64(@_) }
32 1     1 1 4 sub random_bytes_b64u { return $fetch_RNG->()->bytes_b64u(@_) }
33 1     1 1 5 sub random_string_from { return $fetch_RNG->()->string_from(@_) }
34 1     1 1 4 sub random_string { return $fetch_RNG->()->string(@_) }
35             }
36              
37              
38             1;
39              
40             =pod
41              
42             =head1 NAME
43              
44             Crypt::PRNG::Sober128 - Cryptographically secure PRNG based on Sober128 (stream cipher) algorithm
45              
46             =head1 SYNOPSIS
47              
48             ### Functional interface:
49             use Crypt::PRNG::Sober128 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::Sober128;
63              
64             my $prng = Crypt::PRNG::Sober128->new;
65             my $seeded_prng = Crypt::PRNG::Sober128->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 Sober128-based pseudo-random number generator.
80              
81             This is a thin wrapper around L with the algorithm fixed to
82             Sober128. All functions and methods accept the same arguments and return the
83             same values as the corresponding L entries.
84              
85             =head1 EXPORT
86              
87             Nothing is exported by default.
88              
89             You can export selected functions:
90              
91             use Crypt::PRNG::Sober128 qw(random_bytes random_string);
92              
93             Or all of them at once:
94              
95             use Crypt::PRNG::Sober128 ':all';
96              
97             =head1 FUNCTIONS
98              
99             All functions below behave exactly like the corresponding L
100             functions, but use a hidden C object internally.
101              
102             =head2 random_bytes
103              
104             See L.
105              
106             =head2 random_bytes_hex
107              
108             See L.
109              
110             =head2 random_bytes_b64
111              
112             See L.
113              
114             =head2 random_bytes_b64u
115              
116             See L.
117              
118             =head2 random_string
119              
120             See L.
121              
122             =head2 random_string_from
123              
124             See L.
125              
126             =head2 rand
127              
128             See L.
129              
130             =head2 irand
131              
132             See L.
133              
134             =head1 METHODS
135              
136             Unless noted otherwise, assume C<$prng> is an existing
137             C object created via C.
138              
139             =head2 new
140              
141             my $prng = Crypt::PRNG::Sober128->new;
142             my $seeded_prng = Crypt::PRNG::Sober128->new($seed);
143              
144             Creates a PRNG object using the Sober128 algorithm. If C<$seed> is omitted,
145             the object is automatically seeded by the underlying L logic.
146              
147             =head2 bytes
148              
149             See L.
150              
151             =head2 bytes_hex
152              
153             See L.
154              
155             =head2 bytes_b64
156              
157             See L.
158              
159             =head2 bytes_b64u
160              
161             See L.
162              
163             =head2 string
164              
165             See L.
166              
167             =head2 string_from
168              
169             See L.
170              
171             =head2 double
172              
173             See L.
174              
175             =head2 int32
176              
177             See L.
178              
179             =head1 SEE ALSO
180              
181             =over
182              
183             =item * L
184              
185             =item * L
186              
187             =back
188              
189             =cut