xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Digest/base.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage Digest::base;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateuse strict;
4*0Sstevel@tonic-gateuse vars qw($VERSION);
5*0Sstevel@tonic-gate$VERSION = "1.00";
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gate# subclass is supposed to implement at least these
8*0Sstevel@tonic-gatesub new;
9*0Sstevel@tonic-gatesub clone;
10*0Sstevel@tonic-gatesub add;
11*0Sstevel@tonic-gatesub digest;
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gatesub reset {
14*0Sstevel@tonic-gate    my $self = shift;
15*0Sstevel@tonic-gate    $self->new(@_);  # ugly
16*0Sstevel@tonic-gate}
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gatesub addfile {
19*0Sstevel@tonic-gate    my ($self, $handle) = @_;
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate    my $n;
22*0Sstevel@tonic-gate    my $buf = "";
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate    while (($n = read($handle, $buf, 4*1024))) {
25*0Sstevel@tonic-gate        $self->add($buf);
26*0Sstevel@tonic-gate    }
27*0Sstevel@tonic-gate    unless (defined $n) {
28*0Sstevel@tonic-gate	require Carp;
29*0Sstevel@tonic-gate	Carp::croak("Read failed: $!");
30*0Sstevel@tonic-gate    }
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate    $self;
33*0Sstevel@tonic-gate}
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gatesub add_bits {
36*0Sstevel@tonic-gate    my $self = shift;
37*0Sstevel@tonic-gate    my $bits;
38*0Sstevel@tonic-gate    my $nbits;
39*0Sstevel@tonic-gate    if (@_ == 1) {
40*0Sstevel@tonic-gate	my $arg = shift;
41*0Sstevel@tonic-gate	$bits = pack("B*", $arg);
42*0Sstevel@tonic-gate	$nbits = length($arg);
43*0Sstevel@tonic-gate    }
44*0Sstevel@tonic-gate    else {
45*0Sstevel@tonic-gate	($bits, $nbits) = @_;
46*0Sstevel@tonic-gate    }
47*0Sstevel@tonic-gate    if (($nbits % 8) != 0) {
48*0Sstevel@tonic-gate	require Carp;
49*0Sstevel@tonic-gate	Carp::croak("Number of bits must be multiple of 8 for this algorithm");
50*0Sstevel@tonic-gate    }
51*0Sstevel@tonic-gate    return $self->add(substr($bits, 0, $nbits/8));
52*0Sstevel@tonic-gate}
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gatesub hexdigest {
55*0Sstevel@tonic-gate    my $self = shift;
56*0Sstevel@tonic-gate    return unpack("H*", $self->digest(@_));
57*0Sstevel@tonic-gate}
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gatesub b64digest {
60*0Sstevel@tonic-gate    my $self = shift;
61*0Sstevel@tonic-gate    require MIME::Base64;
62*0Sstevel@tonic-gate    my $b64 = MIME::Base64::encode($self->digest(@_), "");
63*0Sstevel@tonic-gate    $b64 =~ s/=+$//;
64*0Sstevel@tonic-gate    return $b64;
65*0Sstevel@tonic-gate}
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate1;
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate__END__
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate=head1 NAME
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gateDigest::base - Digest base class
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate=head1 SYNPOSIS
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate  package Digest::Foo;
78*0Sstevel@tonic-gate  use base 'Digest::base';
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate=head1 DESCRIPTION
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gateThe C<Digest::base> class provide implementations of the methods
83*0Sstevel@tonic-gateC<addfile> and C<add_bits> in terms of C<add>, and of the methods
84*0Sstevel@tonic-gateC<hexdigest> and C<b64digest> in terms of C<digest>.
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gateDigest implementations might want to inherit from this class to get
87*0Sstevel@tonic-gatethis implementations of the alternative I<add> and I<digest> methods.
88*0Sstevel@tonic-gateA minimal subclass needs to implement the following methods by itself:
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate    new
91*0Sstevel@tonic-gate    clone
92*0Sstevel@tonic-gate    add
93*0Sstevel@tonic-gate    digest
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gateThe arguments and expected behaviour of these methods are described in
96*0Sstevel@tonic-gateL<Digest>.
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate=head1 SEE ALSO
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gateL<Digest>
101