Digest::Adler32 - The Adler-32 checksum


Digest-Adler32 documentation Contained in the Digest-Adler32 distribution.

Index


Code Index:

NAME

Top

Digest::Adler32 - The Adler-32 checksum

SYNOPSIS

Top

 use Digest::Adler32;
 $a32 = Digest::Adler32->new;

 # add stuff
 $a32->add($some_data);
 $a32->addfile(*STDIN);

 # get digest
 print "Adler32: ", $a32->hexdigest, "\n";




DESCRIPTION

Top

The Digest::Adler32 module implements the Adler-32 checksum as specified in RFC 1950. The interface provided by this module is specified in Digest, but no functional interface is provided.

A binary digest will be 4 bytes long. A hex digest will be 8 characters long. A base64 digest will be 6 characters long.

SEE ALSO

Top

Digest, Digest::MD5

COPYRIGHT

Top


Digest-Adler32 documentation Contained in the Digest-Adler32 distribution.

package Digest::Adler32;

use strict;
use vars qw($VERSION @ISA);
$VERSION = '0.03';

require Digest::base;
@ISA=qw(Digest::base);

sub new
{
    my $class = shift;
    if (ref $class) {
	$$class = 1;  # reset
	return $class;
    }
    my $adler_state = 1;
    return bless \$adler_state, $class;
}

sub clone {
    my $self = shift;
    my $adler_state = $$self;
    return bless \$adler_state, ref($self);
}

# Based on RFC 1950 section 9

sub add {
    my $self = shift;
    my $s1 = $$self & 0x0000FFFF;
    my $s2 = ($$self >> 16) & 0x0000FFFF;
    for my $buf (@_) {
	for my $c (unpack("C*", $buf)) {
	    $s1 = ($s1 + $c ) % 65521;
	    $s2 = ($s2 + $s1) % 65521;
	}
    }
    $$self = ($s2 << 16) + $s1;
    return $self;
}

sub digest {
    my $self = shift;
    my $digest = pack("N", $$self);
    $$self = 1;  # reset
    return $digest;
}

1;