Compute a string's adler32 checksum

Here is an example to calculate adler32 crc for a string :

#!/usr/bin/perl -w
use Compress::Zlib;        # for adler32()

my $crc;
my $string="https://sites.google.com/site/itmyshare/perl-tips-and-examples/how-to-calculate-adler32-in-perl";
print "Calculating checksum for $string ...\n";
$crc = adler32($string,$crc);
printf("adler32:%s\n",$crc);
exit(0);

Of course, it's easy to modify the script a bit to compute a input string.

Run and see

$./crc.pl
Calculating checksum for https://sites.google.com/site/itmyshare/perl-tips-and-examples/how-to-calculate-adler32-in-perl
adler32:3158123414

Compute a file adler32 checksum

As for how to calculate adler32 for a file, do it this way.

#!/usr/bin/perl

use Compress::Zlib;

$filename=$ARGV[0];
while(<>) {
       $crc=adler32($_,$crc)
       }
       printf("%x %s\n",$crc,$filename);
exit(0);

Run and see

$./adler32.pl ./adler32.pl
64712c9d ./adler32.pl

Perl Adler32 Module

Another way is to use perl module

use Digest::Adler32

It should be pretty much similar with the ways I mentioned compute a file or string md5sum in perl

 

 

Comments powered by CComment