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   86095 use strict;
  3         6  
  3         89  
4 3     3   16 use warnings;
  3         5  
  3         161  
5             our $VERSION = '0.088_004';
6              
7 3     3   13 use base qw(Crypt::PRNG Exporter);
  3         4  
  3         722  
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   13 use CryptX;
  3         4  
  3         862  
13              
14             sub new {
15 5     5 1 144275 my ($class, @args) = @_;
16 5         458 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 8286 sub rand { return $fetch_RNG->()->double(@_) }
28 1000     1000 1 1564 sub irand { return $fetch_RNG->()->int32(@_) }
29 1     1 1 5 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
30 1     1 1 5 sub random_bytes_hex { return $fetch_RNG->()->bytes_hex(@_) }
31 1     1 1 5 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 FUNCTIONS
86              
87             All functions below behave exactly like the corresponding L
88             functions, but use a hidden C object internally.
89              
90             =head2 random_bytes
91              
92             See L.
93              
94             =head2 random_bytes_hex
95              
96             See L.
97              
98             =head2 random_bytes_b64
99              
100             See L.
101              
102             =head2 random_bytes_b64u
103              
104             See L.
105              
106             =head2 random_string
107              
108             See L.
109              
110             =head2 random_string_from
111              
112             See L.
113              
114             =head2 rand
115              
116             See L.
117              
118             =head2 irand
119              
120             See L.
121              
122             =head1 METHODS
123              
124             Unless noted otherwise, assume C<$prng> is an existing
125             C object created via C.
126              
127             =head2 new
128              
129             my $prng = Crypt::PRNG::Sober128->new;
130             my $seeded_prng = Crypt::PRNG::Sober128->new($seed);
131              
132             Creates a PRNG object using the Sober128 algorithm. If C<$seed> is omitted,
133             the object is automatically seeded by the underlying L logic.
134              
135             =head2 bytes
136              
137             See L.
138              
139             =head2 bytes_hex
140              
141             See L.
142              
143             =head2 bytes_b64
144              
145             See L.
146              
147             =head2 bytes_b64u
148              
149             See L.
150              
151             =head2 string
152              
153             See L.
154              
155             =head2 string_from
156              
157             See L.
158              
159             =head2 double
160              
161             See L.
162              
163             =head2 int32
164              
165             See L.
166              
167             =head1 SEE ALSO
168              
169             =over
170              
171             =item * L
172              
173             =item * L
174              
175             =back
176              
177             =cut