Subversion Repositories camp_sysinfo_client_3

Rev

Rev 244 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

# implementation of Fletcher's Checksum
# https://en.wikipedia.org/wiki/Fletcher's_checksum
# Actually, changed it to Adler
# https://en.wikipedia.org/wiki/Adler-32

use strict;
use warnings;

sub checksum {

   my $modulo = 65521;

   my $sum1 = 0;
   my $sum2 = 0;

   while ( my $a = shift ) {
      $sum1 = ( $sum1 + $a) % $modulo;
      $sum2 = ( $sum2 + $sum1 ) % $modulo;
   }
   return ( $sum2 << 16 ) | $sum1;

}

my $input = join( '', <> );

printf ( "%x\n", &checksum( unpack( "C*", $input ) ) );

1;