| 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<Crypt::URandom>, L<Crypt::SysRandom> or L<Crypt::PRNG> 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<rand> function. | ||||||||||
| 31 | |||||||||||
| 32 | Please see CPAN Author's Guide to Random Data for Security | ||||||||||
| 33 | L<https://security.metacpan.org/docs/guides/random-data-for-security.html>. | ||||||||||
| 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<Data::Entropy::Algorithms>, 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<Data::Entropy::RawSource::CryptCounter> | ||||||||||
| 48 | and L<Crypt::Rijndael>), keyed using L<Crypt::URandom>. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | package Data::Entropy; | ||||||||||
| 53 | |||||||||||
| 54 | 11 | ▶ | { use 5.006; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | |||||||||||
| 58 | 11 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 11 | ▶ | use Devel::Deprecate qw(deprecate); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 11 | ▶ | use Params::Classify 0.000 qw(is_ref); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 11 | our $VERSION = "0.010"; | |||||||||
| 63 | |||||||||||
| 64 | 11 | ▶ | use parent "Exporter"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | 11 | our @EXPORT_OK = qw(entropy_source with_entropy_source); | |||||||||
| 66 | |||||||||||
| 67 | 11 | our $entropy_source; | |||||||||
| 68 | |||||||||||
| 69 | =head1 FUNCTIONS | ||||||||||
| 70 | |||||||||||
| 71 | =over | ||||||||||
| 72 | |||||||||||
| 73 | =item entropy_source | ||||||||||
| 74 | |||||||||||
| 75 | Returns the current entropy source, a C<Data::Entropy::Source> | ||||||||||
| 76 | object. This will be the source passed to the innermost call to | ||||||||||
| 77 | C<with_entropy_source>, if any, or otherwise the default entropy source. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 11 | my $default_entropy_source; | |||||||||
| 82 | |||||||||||
| 83 | 21503 | ▶ | sub entropy_source() { | ||||||||
|
Subroutine
entropy_source: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 84 | 21503 | deprecate( reason => 'Please use an alternative module as recommended in the STATUS section' ); | |||||||||
| 85 | 21503 | ▶ | if(is_ref($entropy_source, "CODE")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 2 | my $source = $entropy_source->(); | |||||||||
| 87 | 2 | ▶ | croak "entropy source thunk returned another thunk" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | if is_ref($source, "CODE"); | ||||||||||
| 89 | 2 | $entropy_source = $source; | |||||||||
| 90 | } | ||||||||||
| 91 | 21503 | ▶ | unless(defined $entropy_source) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 4 | ▶ | unless(defined $default_entropy_source) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 2 | require Crypt::URandom; | |||||||||
| 94 | 2 | my $key = Crypt::URandom::urandom(32); | |||||||||
| 95 | 2 | require Crypt::Rijndael; | |||||||||
| 96 | 2 | require Data::Entropy::RawSource::CryptCounter; | |||||||||
| 97 | 2 | require Data::Entropy::Source; | |||||||||
| 98 | 2 | $default_entropy_source = | |||||||||
| 99 | Data::Entropy::Source->new( | ||||||||||
| 100 | Data::Entropy::RawSource::CryptCounter | ||||||||||
| 101 | ->new(Crypt::Rijndael | ||||||||||
| 102 | ->new($key)), | ||||||||||
| 103 | "getc"); | ||||||||||
| 104 | } | ||||||||||
| 105 | 4 | $entropy_source = $default_entropy_source; | |||||||||
| 106 | } | ||||||||||
| 107 | 21503 | return $entropy_source; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =item with_entropy_source SOURCE, CLOSURE | ||||||||||
| 111 | |||||||||||
| 112 | The SOURCE is selected, so that it will be returned by C<entropy_source>, | ||||||||||
| 113 | and CLOSURE is called (with no arguments). The SOURCE is selected only | ||||||||||
| 114 | during the dynamic scope of the call; after CLOSURE finishes, by whatever | ||||||||||
| 115 | means, the previously selected entropy source is restored. | ||||||||||
| 116 | |||||||||||
| 117 | SOURCE is normally a C<Data::Entropy::Source> object. Alternatively, | ||||||||||
| 118 | it may be C<undef> to cause use of the default entropy source. It may | ||||||||||
| 119 | also be a reference to a function of no arguments, which will be called to | ||||||||||
| 120 | generate the actual source only if required. This avoids unnecessarily | ||||||||||
| 121 | initialising the source object if it is uncertain whether any entropy | ||||||||||
| 122 | will be required. The source-generating closure may return a normal | ||||||||||
| 123 | source or C<undef>, but not another function reference. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||
| 126 | |||||||||||
| 127 | 14 | ▶ | sub with_entropy_source($&) { | ||||||||
|
Subroutine
with_entropy_source: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 128 | 14 | my($source, $closure) = @_; | |||||||||
| 129 | 14 | local $entropy_source = $source; | |||||||||
| 130 | 14 | $closure->(); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | =back | ||||||||||
| 134 | |||||||||||
| 135 | =head1 SEE ALSO | ||||||||||
| 136 | |||||||||||
| 137 | L<Data::Entropy::Algorithms>, | ||||||||||
| 138 | L<Data::Entropy::Source> | ||||||||||
| 139 | |||||||||||
| 140 | =head1 AUTHOR | ||||||||||
| 141 | |||||||||||
| 142 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 143 | |||||||||||
| 144 | Maintained by Robert Rothenberg <perl@rhizomnic.com> | ||||||||||
| 145 | |||||||||||
| 146 | =head1 COPYRIGHT | ||||||||||
| 147 | |||||||||||
| 148 | Copyright (C) 2006, 2007, 2009, 2011, 2025-2026 | ||||||||||
| 149 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 150 | |||||||||||
| 151 | =head1 LICENSE | ||||||||||
| 152 | |||||||||||
| 153 | This module is free software; you can redistribute it and/or modify it | ||||||||||
| 154 | under the same terms as Perl itself. | ||||||||||
| 155 | |||||||||||
| 156 | =cut | ||||||||||
| 157 | |||||||||||
| 158 | 11 | 1; | |||||||||