File Coverage

blib/lib/Data/Entropy.pm
Criterion Covered Total %
statement 34 34 100.0
branch 7 8 87.5
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Data::Entropy - entropy (randomness) management
4              
5             =head1 SYNOPSIS
6              
7             use Data::Entropy qw(entropy_source);
8              
9             $i = entropy_source->get_int(12345);
10              
11             use Data::Entropy qw(with_entropy_source);
12              
13             with_entropy_source $source, sub {
14             @a = shuffle(@a);
15             };
16              
17             =head1 STATUS
18              
19             This module is deprecated.
20              
21             For most purposes (including cryptography and security), modules like
22             L, L or L are more than
23             adequate.
24              
25             Modern operating systems provide good sources of random bytes, and
26             the above mentioned modules work on many kinds of systems, including Windows.
27              
28             There is no need to choose an entropy source, and some users of this
29             module have omitted that step, and prior to version 0.008 they may
30             have been relying on Perl's builtin C function.
31              
32             Please see CPAN Author's Guide to Random Data for Security
33             L.
34              
35             =head1 DESCRIPTION
36              
37             This module maintains a concept of a current selection of
38             entropy source. Algorithms that require entropy, such as those in
39             L, can use the source nominated by this
40             module, avoiding the need for entropy source objects to be explicitly
41             passed around. This is convenient because usually one entropy source
42             will be used for an entire program run and so an explicit entropy source
43             parameter would rarely vary. There is also a default entropy source,
44             avoiding the need to explicitly configure a source at all.
45              
46             If nothing is done to set a source then it defaults to the use of Rijndael
47             (AES) in counter mode (see L
48             and L), keyed using L.
49              
50             =cut
51              
52             package Data::Entropy;
53              
54 11     11   363951 { use 5.006; }
  11         39  
55 11     11   66 use warnings;
  11         37  
  11         554  
56 11     11   50 use strict;
  11         21  
  11         330  
57              
58 11     11   90 use Carp qw(croak);
  11         29  
  11         748  
59 11     11   5383 use Params::Classify 0.000 qw(is_ref);
  11         36520  
  11         1215  
60              
61             our $VERSION = "0.008";
62              
63 11     11   118 use parent "Exporter";
  11         21  
  11         43  
64             our @EXPORT_OK = qw(entropy_source with_entropy_source);
65              
66             our $entropy_source;
67              
68             =head1 FUNCTIONS
69              
70             =over
71              
72             =item entropy_source
73              
74             Returns the current entropy source, a C
75             object. This will be the source passed to the innermost call to
76             C, if any, or otherwise the default entropy source.
77              
78             =cut
79              
80             my $default_entropy_source;
81              
82             sub entropy_source() {
83 21477 100   21477 1 58611 if(is_ref($entropy_source, "CODE")) {
84 2         5 my $source = $entropy_source->();
85 2 50       15 croak "entropy source thunk returned another thunk"
86             if is_ref($source, "CODE");
87 2         4 $entropy_source = $source;
88             }
89 21477 100       47931 unless(defined $entropy_source) {
90 4 100       12 unless(defined $default_entropy_source) {
91 2         1693 require Crypt::URandom;
92 2         11431 my $key = Crypt::URandom::urandom(32);
93 2         1254 require Crypt::Rijndael;
94 2         2820 require Data::Entropy::RawSource::CryptCounter;
95 2         1298 require Data::Entropy::Source;
96 2         66 $default_entropy_source =
97             Data::Entropy::Source->new(
98             Data::Entropy::RawSource::CryptCounter
99             ->new(Crypt::Rijndael
100             ->new($key)),
101             "getc");
102             }
103 4         11 $entropy_source = $default_entropy_source;
104             }
105 21477         74796 return $entropy_source;
106             }
107              
108             =item with_entropy_source SOURCE, CLOSURE
109              
110             The SOURCE is selected, so that it will be returned by C,
111             and CLOSURE is called (with no arguments). The SOURCE is selected only
112             during the dynamic scope of the call; after CLOSURE finishes, by whatever
113             means, the previously selected entropy source is restored.
114              
115             SOURCE is normally a C object. Alternatively,
116             it may be C to cause use of the default entropy source. It may
117             also be a reference to a function of no arguments, which will be called to
118             generate the actual source only if required. This avoids unnecessarily
119             initialising the source object if it is uncertain whether any entropy
120             will be required. The source-generating closure may return a normal
121             source or C, but not another function reference.
122              
123             =cut
124              
125             sub with_entropy_source($&) {
126 14     14 1 243165 my($source, $closure) = @_;
127 14         31 local $entropy_source = $source;
128 14         51 $closure->();
129             }
130              
131             =back
132              
133             =head1 SEE ALSO
134              
135             L,
136             L
137              
138             =head1 AUTHOR
139              
140             Andrew Main (Zefram)
141              
142             Maintained by Robert Rothenberg
143              
144             =head1 COPYRIGHT
145              
146             Copyright (C) 2006, 2007, 2009, 2011, 2025
147             Andrew Main (Zefram)
148              
149             =head1 LICENSE
150              
151             This module is free software; you can redistribute it and/or modify it
152             under the same terms as Perl itself.
153              
154             =cut
155              
156             1;