# PERL - this assumes that the checksum is the same across file types.
sub get_checksum {
my @chars = split // , shift;
my @ord_chars = map { ord } @chars; # get a number for each letter
my @odd = ();
my @even = ();
# odd and even characters are handled differently.
foreach my $i (0..$#chars) {
if ( $i%2 ) {
push @odd, $ord_chars[$i];
} else {
push @even, $ord_chars[$i];
}
}
my $odd_sum = 0;
my $even_sum = 0;
# odd characters have their numeric representation doubled
$odd_sum += 2*$_ for @odd;
$even_sum += $_ for @even;
# the checksum formula is below:
# 1) Take the integer part of division by 21 of the sum of the characters (2*odd + even)
# 2) Add 205
# 3) The first digit of the checksum is the last digit of the previous result
# 4) The second digit of the checksum is the penultimate digit of the same
my $sum = $odd_sum + $even_sum;
my $result = ($sum - $sum%21) / 21;
my $sum2 = $result + 205;
my $checksum1 = substr($sum2,-1);
my $checksum2 = substr($sum2,-2,1);
my $checksum = "$checksum1$checksum2";
return $checksum;