| 1 | package Crypt::Random::Source::Factory; | ||||||||||||
| 2 | # ABSTRACT: Load and instantiate sources of random data | ||||||||||||
| 3 | |||||||||||||
| 4 | 2 | our $VERSION = '0.14'; | |||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Module::Find; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Module::Runtime qw(require_module); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Types::Standard qw(ClassName Bool ArrayRef Str); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | ▶ | sub get { | ||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 14 | 4 | my ( $self, %args ) = @_; | |||||||||||
| 15 | |||||||||||||
| 16 | 4 | ▶ | my $type = delete $args{type} || "any"; | ||||||||||
|
Condition100%2/2
delete $args{'type'} || 'any'
MC/DC100%1/1
delete $args{'type'} || 'any'
delete $args{'type'}
Truth table100%2/2
A
A= delete $args{'type'}
| |||||||||||||
| 17 | |||||||||||||
| 18 | 4 | my $method = "new_$type"; | |||||||||||
| 19 | |||||||||||||
| 20 | 4 | ▶ | $self->can($method) or croak "Don't know how to create a source of type $type"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | |||||||||||||
| 22 | 4 | $self->$method(%args); | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | 2 | ▶ | sub get_weak { | ||||||||||
|
Subroutine
get_weak: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 26 | 2 | my ( $self, @args ) = @_; | |||||||||||
| 27 | 2 | $self->get( @args, type => "weak" ); | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | 1 | ▶ | sub get_strong { | ||||||||||
|
Subroutine
get_strong: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 31 | 1 | my ( $self, @args ) = @_; | |||||||||||
| 32 | 1 | $self->get( @args, type => "strong" ); | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 2 | has weak_source => ( | |||||||||||
| 36 | isa => ClassName, | ||||||||||||
| 37 | is => "rw", | ||||||||||||
| 38 | lazy => 1, | ||||||||||||
| 39 | builder => 1, | ||||||||||||
| 40 | clearer => "clear_weak_source", | ||||||||||||
| 41 | predicate => "has_weak_source", | ||||||||||||
| 42 | handles => { new_weak => "new" }, | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | 2 | ▶ | sub _build_weak_source { | ||||||||||
|
Subroutine
_build_weak_source: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 2 | my $self = shift; | |||||||||||
| 47 | 2 | $self->best_available(@{ $self->weak_sources }); | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 2 | has strong_source => ( | |||||||||||
| 51 | isa => ClassName, | ||||||||||||
| 52 | is => "rw", | ||||||||||||
| 53 | lazy => 1, | ||||||||||||
| 54 | builder => 1, | ||||||||||||
| 55 | clearer => "clear_strong_source", | ||||||||||||
| 56 | predicate => 'has_strong_source', | ||||||||||||
| 57 | handles => { new_strong => "new" }, | ||||||||||||
| 58 | ); | ||||||||||||
| 59 | |||||||||||||
| 60 | 1 | ▶ | sub _build_strong_source { | ||||||||||
|
Subroutine
_build_strong_source: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 1 | my $self = shift; | |||||||||||
| 62 | 1 | $self->best_available(@{ $self->strong_sources }); | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 2 | has any_source => ( | |||||||||||
| 66 | isa => ClassName, | ||||||||||||
| 67 | is => "rw", | ||||||||||||
| 68 | lazy => 1, | ||||||||||||
| 69 | builder => 1, | ||||||||||||
| 70 | clearer => "clear_any_source", | ||||||||||||
| 71 | predicate => 'has_any_source', | ||||||||||||
| 72 | handles => { new_any => 'new' }, | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | |||||||||||||
| 75 | 1 | ▶ | sub _build_any_source { | ||||||||||
|
Subroutine
_build_any_source: called CC=2 SCAR=7.2
| |||||||||||||
| 76 | 1 | my $self = shift; | |||||||||||
| 77 | 1 | ▶ | $self->weak_source || $self->strong_source; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 2 | has scan_inc => ( | |||||||||||
| 81 | is => "ro", | ||||||||||||
| 82 | isa => Bool, | ||||||||||||
| 83 | lazy => 1, | ||||||||||||
| 84 | builder => 1, | ||||||||||||
| 85 | clearer => 'clear_scan_inc', | ||||||||||||
| 86 | predicate => 'has_scan_inc', | ||||||||||||
| 87 | ); | ||||||||||||
| 88 | |||||||||||||
| 89 | 2 | ▶ | sub _build_scan_inc { | ||||||||||
|
Subroutine
_build_scan_inc: called CC=2 SCAR=7.6
| |||||||||||||
| 90 | 2 | my $self = shift; | |||||||||||
| 91 | |||||||||||||
| 92 | 2 | ▶ | if ( exists $ENV{CRYPT_RANDOM_NOT_PLUGGABLE} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 0 | return !$ENV{CRYPT_RANDOM_NOT_PLUGGABLE}; | |||||||||||
| 94 | } else { | ||||||||||||
| 95 | 2 | return 1; | |||||||||||
| 96 | } | ||||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 2 | has weak_sources => ( | |||||||||||
| 100 | isa => ArrayRef[Str], | ||||||||||||
| 101 | is => "rw", | ||||||||||||
| 102 | lazy => 1, | ||||||||||||
| 103 | builder => 1, | ||||||||||||
| 104 | clearer => 'clear_weak_sources', | ||||||||||||
| 105 | predicate => 'has_weak_sources', | ||||||||||||
| 106 | ); | ||||||||||||
| 107 | |||||||||||||
| 108 | 2 | ▶ | sub _build_weak_sources { | ||||||||||
|
Subroutine
_build_weak_sources: called CC=2 SCAR=7.6
| |||||||||||||
| 109 | 2 | my $self = shift; | |||||||||||
| 110 | |||||||||||||
| 111 | 2 | ▶ | if ( $self->scan_inc ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 2 | $self->locate_sources("Weak"); | |||||||||||
| 113 | } else { | ||||||||||||
| 114 | 0 | return [qw( | |||||||||||
| 115 | Crypt::Random::Source::Weak::devurandom | ||||||||||||
| 116 | Crypt::Random::Source::Weak::openssl | ||||||||||||
| 117 | )]; | ||||||||||||
| 118 | } | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 2 | has strong_sources => ( | |||||||||||
| 122 | isa => ArrayRef[Str], | ||||||||||||
| 123 | is => "rw", | ||||||||||||
| 124 | lazy => 1, | ||||||||||||
| 125 | builder => 1, | ||||||||||||
| 126 | clearer => 'clear_strong_sources', | ||||||||||||
| 127 | predicate => 'has_strong_sources', | ||||||||||||
| 128 | ); | ||||||||||||
| 129 | |||||||||||||
| 130 | 1 | ▶ | sub _build_strong_sources { | ||||||||||
|
Subroutine
_build_strong_sources: called CC=2 SCAR=7.6
| |||||||||||||
| 131 | 1 | my $self = shift; | |||||||||||
| 132 | |||||||||||||
| 133 | 1 | ▶ | if ( $self->scan_inc ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 1 | return $self->locate_sources("Strong"); | |||||||||||
| 135 | } else { | ||||||||||||
| 136 | 0 | return [qw( | |||||||||||
| 137 | Crypt::Random::Source::Strong::devrandom | ||||||||||||
| 138 | Crypt::Random::Source::Strong::egd | ||||||||||||
| 139 | )]; | ||||||||||||
| 140 | } | ||||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 3 | ▶ | sub best_available { | ||||||||||
|
Subroutine
best_available: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 144 | 3 | my ( $self, @sources ) = @_; | |||||||||||
| 145 | |||||||||||||
| 146 | 3 | my @available = grep { local $@; eval { require_module($_); $_->available }; } @sources; | |||||||||||
| 147 | |||||||||||||
| 148 | 0 | my @sorted = sort { $b->rank <=> $a->rank } @available; | |||||||||||
| 149 | |||||||||||||
| 150 | 3 | ▶ | wantarray ? @sorted : $sorted[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 0 | ▶ | sub first_available { | ||||||||||
|
Subroutine
first_available: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 154 | 0 | my ( $self, @sources ) = @_; | |||||||||||
| 155 | |||||||||||||
| 156 | 0 | foreach my $class ( @sources ) { | |||||||||||
| 157 | 0 | local $@; | |||||||||||
| 158 | 0 | ▶ | return $class if eval { require_module($class); $class->available }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | } | ||||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 3 | ▶ | sub locate_sources { | ||||||||||
|
Subroutine
locate_sources: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 163 | 3 | my ( $self, $category ) = @_; | |||||||||||
| 164 | 3 | my @sources = findsubmod "Crypt::Random::Source::$category"; | |||||||||||
| 165 | # Untaint class names (which are tainted in taint mode because | ||||||||||||
| 166 | # they came from the disk). | ||||||||||||
| 167 | 3 | ($_) = $_ =~ /^(.*)$/ foreach @sources; | |||||||||||
| 168 | 3 | return \@sources; | |||||||||||
| 169 | } | ||||||||||||
| 170 | |||||||||||||
| 171 | 2 | 1; | |||||||||||
| 172 | |||||||||||||
| 173 | =pod | ||||||||||||
| 174 | |||||||||||||
| 175 | =encoding UTF-8 | ||||||||||||
| 176 | |||||||||||||
| 177 | =head1 NAME | ||||||||||||
| 178 | |||||||||||||
| 179 | Crypt::Random::Source::Factory - Load and instantiate sources of random data | ||||||||||||
| 180 | |||||||||||||
| 181 | =head1 VERSION | ||||||||||||
| 182 | |||||||||||||
| 183 | version 0.14 | ||||||||||||
| 184 | |||||||||||||
| 185 | =head1 SYNOPSIS | ||||||||||||
| 186 | |||||||||||||
| 187 | use Crypt::Random::Source::Factory; | ||||||||||||
| 188 | |||||||||||||
| 189 | my $f = Crypt::Random::Source::Factory->new; | ||||||||||||
| 190 | |||||||||||||
| 191 | my $strong = $f->get_strong; | ||||||||||||
| 192 | |||||||||||||
| 193 | my $weak = $f->get_weak; | ||||||||||||
| 194 | |||||||||||||
| 195 | my $any = $f->get; | ||||||||||||
| 196 | |||||||||||||
| 197 | =head1 DESCRIPTION | ||||||||||||
| 198 | |||||||||||||
| 199 | This class implements a loading and instantiation factory for | ||||||||||||
| 200 | L<Crypt::Random::Source> objects. | ||||||||||||
| 201 | |||||||||||||
| 202 | If C<$ENV{CRYPT_RANDOM_NOT_PLUGGABLE}> is set then only a preset list of | ||||||||||||
| 203 | sources will be tried. Otherwise L<Module::Find> will be used to locate any | ||||||||||||
| 204 | installed sources, and use the first available one. | ||||||||||||
| 205 | |||||||||||||
| 206 | =head1 METHODS | ||||||||||||
| 207 | |||||||||||||
| 208 | =head2 get %args | ||||||||||||
| 209 | |||||||||||||
| 210 | Instantiate any random source, passing %args to the constructor. | ||||||||||||
| 211 | |||||||||||||
| 212 | The C<type> argument can be C<weak>, C<strong> or C<any>. | ||||||||||||
| 213 | |||||||||||||
| 214 | =head2 get_weak %args | ||||||||||||
| 215 | |||||||||||||
| 216 | =head2 get_strong %args | ||||||||||||
| 217 | |||||||||||||
| 218 | Instantiate a new weak or strong random source. | ||||||||||||
| 219 | |||||||||||||
| 220 | =head1 SUPPORT | ||||||||||||
| 221 | |||||||||||||
| 222 | Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Crypt-Random-Source> | ||||||||||||
| 223 | (or L<bug-Crypt-Random-Source@rt.cpan.org|mailto:bug-Crypt-Random-Source@rt.cpan.org>). | ||||||||||||
| 224 | |||||||||||||
| 225 | =head1 AUTHOR | ||||||||||||
| 226 | |||||||||||||
| 227 | יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> | ||||||||||||
| 228 | |||||||||||||
| 229 | =head1 COPYRIGHT AND LICENCE | ||||||||||||
| 230 | |||||||||||||
| 231 | This software is copyright (c) 2008 by Yuval Kogman. | ||||||||||||
| 232 | |||||||||||||
| 233 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 234 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 235 | |||||||||||||
| 236 | =cut | ||||||||||||
| 237 | |||||||||||||
| 238 | __END__ | ||||||||||||