File Coverage

blib/lib/Crypt/PRNG/RC4.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::RC4;
2              
3 3     3   128782 use strict;
  3         5  
  3         84  
4 3     3   13 use warnings;
  3         4  
  3         153  
5             our $VERSION = '0.089';
6              
7 3     3   15 use base qw(Crypt::PRNG Exporter);
  3         11  
  3         662  
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         5  
  3         911  
13              
14             sub new {
15 5     5 1 173286 my ($class, @args) = @_;
16 5         460 my $obj = Crypt::PRNG->new('RC4', @args);
17 5         44 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::RC4->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
25             return $RNG_object;
26             };
27 2000     2000 1 9364 sub rand { return $fetch_RNG->()->double(@_) }
28 1000     1000 1 1497 sub irand { return $fetch_RNG->()->int32(@_) }
29 1     1 1 9 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::RC4 - Legacy RC4-based PRNG wrapper
45              
46             =head1 SYNOPSIS
47              
48             ### Functional interface:
49             use Crypt::PRNG::RC4 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::RC4;
63              
64             my $prng = Crypt::PRNG::RC4->new;
65             my $seeded_prng = Crypt::PRNG::RC4->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 RC4-based pseudo-random number generator.
80              
81             This is a thin wrapper around L with the algorithm fixed to RC4.
82             All functions and methods accept the same arguments and return the same values
83             as the corresponding L entries.
84              
85             RC4 is provided for compatibility with legacy code only and is not recommended
86             for new designs.
87              
88             =head1 EXPORT
89              
90             Nothing is exported by default.
91              
92             You can export selected functions:
93              
94             use Crypt::PRNG::RC4 qw(random_bytes random_string);
95              
96             Or all of them at once:
97              
98             use Crypt::PRNG::RC4 ':all';
99              
100             =head1 FUNCTIONS
101              
102             All functions below behave exactly like the corresponding L
103             functions, but use a hidden C object internally.
104              
105             =head2 random_bytes
106              
107             See L.
108              
109             =head2 random_bytes_hex
110              
111             See L.
112              
113             =head2 random_bytes_b64
114              
115             See L.
116              
117             =head2 random_bytes_b64u
118              
119             See L.
120              
121             =head2 random_string
122              
123             See L.
124              
125             =head2 random_string_from
126              
127             See L.
128              
129             =head2 rand
130              
131             See L.
132              
133             =head2 irand
134              
135             See L.
136              
137             =head1 METHODS
138              
139             Unless noted otherwise, assume C<$prng> is an existing C
140             object created via C.
141              
142             =head2 new
143              
144             my $prng = Crypt::PRNG::RC4->new;
145             my $seeded_prng = Crypt::PRNG::RC4->new($seed);
146              
147             Creates a PRNG object using the RC4 algorithm. If C<$seed> is omitted, the
148             object is automatically seeded by the underlying L logic. If
149             C<$seed> is provided it must be at least 5 bytes long; empty or shorter seeds
150             fail during initialization.
151              
152             =head2 bytes
153              
154             See L.
155              
156             =head2 bytes_hex
157              
158             See L.
159              
160             =head2 bytes_b64
161              
162             See L.
163              
164             =head2 bytes_b64u
165              
166             See L.
167              
168             =head2 string
169              
170             See L.
171              
172             =head2 string_from
173              
174             See L.
175              
176             =head2 double
177              
178             See L.
179              
180             =head2 int32
181              
182             See L.
183              
184             =head1 SEE ALSO
185              
186             =over
187              
188             =item * L
189              
190             =item * L
191              
192             =back
193              
194             =cut