1package Crypt::Random::Source::Base;
2# ABSTRACT: Abstract base class for L<Crypt::Random::Source> classes
3
4our $VERSION = '0.12';
5
6use Moo;
7use namespace::clean;
8
9sub available { 0 }
10
11sub rank { 0 }
12
13sub seed { }
14
15sub get { die "abstract" }
16
17# cannibalized from IO::Scalar
18sub read {
19    my $self = $_[0];
20    my $n    = $_[2];
21    my $off  = $_[3] || 0;
22
23    my $read = $self->get($n);
24    $n = length($read);
25    ($off ? substr($_[1], $off) : $_[1]) = $read;
26    return $n;
27}
28
29sub get_data {
30    my ( $self, %params ) = @_;
31
32    if ( my $n = $params{Length} ) {
33        return $self->get($n);
34    } else {
35        my $size = $params{Size};
36
37        if (ref $size && ref $size eq "Math::Pari") {
38            $size = Math::Pari::pari2num($size);
39        }
40
41        return $self->get( int($size / 8) + 1 );
42    }
43}
44
451;
46
47=pod
48
49=encoding UTF-8
50
51=head1 NAME
52
53Crypt::Random::Source::Base - Abstract base class for L<Crypt::Random::Source> classes
54
55=head1 VERSION
56
57version 0.12
58
59=head1 SYNOPSIS
60
61    use Moo;
62    extends qw(Crypt::Random::Source::Base);
63
64=head1 DESCRIPTION
65
66This is an abstract base class.
67
68In the future it will be a role.
69
70=head1 METHODS
71
72=head2 get $n, %args
73
74Gets C<$n> random bytes and returns them as a string.
75
76This method may produce fatal errors if the source was unable to provide enough
77data.
78
79=head2 read $buf, $n, [ $off ]
80
81This method is cannibalized from L<IO::Scalar>. It provides an L<IO::Handle>
82work-alike.
83
84Note that subclasses override this to operate on a real handle directly if
85available.
86
87=head2 seed @stuff
88
89On supporting sources this method will add C<@stuff>, whatever it may be, to
90the random seed.
91
92Some sources may not support this, so be careful.
93
94=head2 available
95
96This is a class method, such that when it returns true calling C<new> without
97arguments on the class should provide a working source of random data.
98
99This is use by L<Crypt::Random::Source::Factory>.
100
101=head2 rank
102
103This is a class method, with some futz value for a ranking, to help known good
104sources be tried before known bad (slower, less available) sources.
105
106=head2 get_data %Params
107
108Provided for compatibility with L<Crypt::Random>
109
110=head1 SUPPORT
111
112Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Crypt-Random-Source>
113(or L<bug-Crypt-Random-Source@rt.cpan.org|mailto:bug-Crypt-Random-Source@rt.cpan.org>).
114
115=head1 AUTHOR
116
117יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
118
119=head1 COPYRIGHT AND LICENCE
120
121This software is copyright (c) 2008 by Yuval Kogman.
122
123This is free software; you can redistribute it and/or modify it under
124the same terms as the Perl 5 programming language system itself.
125
126=cut
127
128__END__
129
130
131# ex: set sw=4 et:
132