File Coverage

blib/lib/Crypt/PRNG/Fortuna.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::Fortuna;
2              
3 3     3   93753 use strict;
  3         6  
  3         111  
4 3     3   16 use warnings;
  3         5  
  3         186  
5             our $VERSION = '0.088_004';
6              
7 3     3   12 use base qw(Crypt::PRNG Exporter);
  3         4  
  3         857  
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   15 use CryptX;
  3         3  
  3         771  
13              
14             sub new {
15 5     5 1 272246 my ($class, @args) = @_;
16 5         509 my $obj = Crypt::PRNG->new('Fortuna', @args);
17 5         51 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::Fortuna->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
25             return $RNG_object;
26             };
27 2000     2000 1 9684 sub rand { return $fetch_RNG->()->double(@_) }
28 1000     1000 1 1626 sub irand { return $fetch_RNG->()->int32(@_) }
29 1     1 1 4 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 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 4 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::Fortuna - Cryptographically secure PRNG based on Fortuna algorithm
45              
46             =head1 SYNOPSIS
47              
48             ### Functional interface:
49             use Crypt::PRNG::Fortuna qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u
50             random_string random_string_from rand irand);
51              
52             my $octets = random_bytes(45);
53             my $hex_string = random_bytes_hex(45);
54             my $base64_string = random_bytes_b64(45);
55             my $base64url_string = random_bytes_b64u(45);
56             my $alphanumeric_string = random_string(30);
57             my $string = random_string_from('ACGT', 64);
58             my $floating_point_number_0_to_1 = rand;
59             my $floating_point_number_0_to_88 = rand(88);
60             my $unsigned_32bit_int = irand;
61              
62             ### OO interface:
63             use Crypt::PRNG::Fortuna;
64              
65             my $prng = Crypt::PRNG::Fortuna->new;
66             my $seeded_prng = Crypt::PRNG::Fortuna->new("some data used for seeding PRNG");
67              
68             my $octets = $prng->bytes(45);
69             my $hex_string = $prng->bytes_hex(45);
70             my $base64_string = $prng->bytes_b64(45);
71             my $base64url_string = $prng->bytes_b64u(45);
72             my $alphanumeric_string = $prng->string(30);
73             my $string = $prng->string_from('ACGT', 64);
74             my $floating_point_number_0_to_1 = $prng->double;
75             my $floating_point_number_0_to_88 = $prng->double(88);
76             my $unsigned_32bit_int = $prng->int32;
77              
78             =head1 DESCRIPTION
79              
80             Provides an interface to the Fortuna-based pseudo-random number generator.
81              
82             This is a thin wrapper around L with the algorithm fixed to
83             Fortuna. All functions and methods accept the same arguments and return the
84             same values as the corresponding L entries.
85              
86             =head1 FUNCTIONS
87              
88             All functions below behave exactly like the corresponding L
89             functions, but use a hidden C object internally.
90              
91             =head2 random_bytes
92              
93             See L.
94              
95             =head2 random_bytes_hex
96              
97             See L.
98              
99             =head2 random_bytes_b64
100              
101             See L.
102              
103             =head2 random_bytes_b64u
104              
105             See L.
106              
107             =head2 random_string
108              
109             See L.
110              
111             =head2 random_string_from
112              
113             See L.
114              
115             =head2 rand
116              
117             See L.
118              
119             =head2 irand
120              
121             See L.
122              
123             =head1 METHODS
124              
125             Unless noted otherwise, assume C<$prng> is an existing
126             C object created via C.
127              
128             =head2 new
129              
130             my $prng = Crypt::PRNG::Fortuna->new;
131             my $seeded_prng = Crypt::PRNG::Fortuna->new($seed);
132              
133             Creates a PRNG object using the Fortuna algorithm. If C<$seed> is omitted, the
134             object is automatically seeded by the underlying L logic.
135              
136             =head2 bytes
137              
138             See L.
139              
140             =head2 bytes_hex
141              
142             See L.
143              
144             =head2 bytes_b64
145              
146             See L.
147              
148             =head2 bytes_b64u
149              
150             See L.
151              
152             =head2 string
153              
154             See L.
155              
156             =head2 string_from
157              
158             See L.
159              
160             =head2 double
161              
162             See L.
163              
164             =head2 int32
165              
166             See L.
167              
168             =head1 SEE ALSO
169              
170             =over
171              
172             =item * L
173              
174             =item * L
175              
176             =back
177              
178             =cut