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