line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl -sw |
2
|
|
|
|
|
|
|
## |
3
|
|
|
|
|
|
|
## |
4
|
|
|
|
|
|
|
## |
5
|
|
|
|
|
|
|
## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. |
6
|
|
|
|
|
|
|
## This code is free software; you can redistribute it and/or modify |
7
|
|
|
|
|
|
|
## it under the same terms as Perl itself. |
8
|
|
|
|
|
|
|
## |
9
|
|
|
|
|
|
|
## $Id: Generator.pm,v 1.2 2001/06/22 03:43:51 vipul Exp $ |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
package Crypt::Random::Generator; |
12
|
1
|
|
|
1
|
|
7504
|
use Crypt::Random qw(makerandom makerandom_itv makerandom_octet); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Carp; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my @PROVIDERS = qw(devrandom devurandom egd rand); |
16
|
|
|
|
|
|
|
my %STRENGTH = ( 0 => [ qw(devurandom egd rand) ], 1 => [ qw(devrandom egd rand) ] ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my ($class, %params) = @_; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $self = { _STRENGTH => \%STRENGTH, _PROVIDERS => \@PROVIDERS }; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$$self{Strength} = $params{Strength} || 0; |
25
|
|
|
|
|
|
|
$$self{Provider} = $params{Provider} || ""; |
26
|
|
|
|
|
|
|
$$self{ProviderParams} = $params{ProviderParams} || ""; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
bless $self, $class; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
unless ($$self{Provider}) { |
31
|
|
|
|
|
|
|
SELECT_PROVIDER: for ($self->strength_order($$self{Strength})) { |
32
|
|
|
|
|
|
|
my $pname = $_; my $fqpname = "Crypt::Random::Provider::$pname"; |
33
|
|
|
|
|
|
|
if (eval "use $fqpname; $fqpname->available()") { |
34
|
|
|
|
|
|
|
if (grep { $pname eq $_ } $self->providers) { |
35
|
|
|
|
|
|
|
$$self{Provider} = $pname; |
36
|
|
|
|
|
|
|
last SELECT_PROVIDER; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
croak "No provider available.\n" unless $$self{Provider}; |
43
|
|
|
|
|
|
|
return $self; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub providers { |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my ($self, @args) = @_; |
51
|
|
|
|
|
|
|
if (@args) { $$self{_PROVIDERS} = [@args] } |
52
|
|
|
|
|
|
|
return @{$$self{_PROVIDERS}}; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub strength_order { |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my ($self, $strength, @args) = @_; |
60
|
|
|
|
|
|
|
if (@args) { $$self{_STRENGTH}{$strength} = [@args] } |
61
|
|
|
|
|
|
|
return @{$$self{_STRENGTH}{$strength}} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub integer { |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my ($self, %params) = @_; |
69
|
|
|
|
|
|
|
if ($params{Size}) { |
70
|
|
|
|
|
|
|
return makerandom ( |
71
|
|
|
|
|
|
|
Size => $params{Size}, |
72
|
|
|
|
|
|
|
Provider => $$self{Provider}, |
73
|
|
|
|
|
|
|
Verbosity => $params{Verbosity} || $$self{Verbosity}, |
74
|
|
|
|
|
|
|
%{$$self{ProviderParams}}, |
75
|
|
|
|
|
|
|
) |
76
|
|
|
|
|
|
|
} elsif ($params{Upper}) { |
77
|
|
|
|
|
|
|
return makerandom_itv ( |
78
|
|
|
|
|
|
|
Lower => $params{Lower} || 0, |
79
|
|
|
|
|
|
|
Upper => $params{Upper}, |
80
|
|
|
|
|
|
|
Provider => $$self{Provider}, |
81
|
|
|
|
|
|
|
Verbosity => $params{Verbosity} || $$self{Verbosity}, |
82
|
|
|
|
|
|
|
%{$$self{ProviderParams}}, |
83
|
|
|
|
|
|
|
) |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub string { |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my ($self, %params) = @_; |
92
|
|
|
|
|
|
|
return makerandom_octet ( |
93
|
|
|
|
|
|
|
%params, |
94
|
|
|
|
|
|
|
Provider => $$self{Provider}, |
95
|
|
|
|
|
|
|
Verbosity => $params{Verbosity} || $$self{Verbosity}, |
96
|
|
|
|
|
|
|
%{$$self{ProviderParams}}, |
97
|
|
|
|
|
|
|
) |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|