Last active
April 30, 2017 20:48
-
-
Save dpavlin/1048047 to your computer and use it in GitHub Desktop.
JMBAG checksum validator/calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
sub jmbag_check { | |
my $jmbag = shift; | |
$jmbag = sprintf "%09d", $jmbag if length $jmbag < 9; | |
my $sum = 0; | |
my $pos = 0; | |
foreach my $tezina ( 2,3,4,5,1,2,3,4,5 ) { | |
my $nr = substr($jmbag,$pos,1); | |
$sum += $nr * $tezina; | |
warn "# [$pos] $nr * $tezina ~ $sum\n"; | |
$pos++; | |
} | |
$sum = 11 - ( $sum % 11 ); | |
die "invalid" if $sum == 10; | |
$sum = 0 if $sum == 11; | |
if ( length $jmbag == 10 ) { | |
my $ck = substr $jmbag,9,1; | |
die "invalid JMBAG $jmbag [ $ck != $sum ]" if $ck != $sum; | |
warn "OK $jmbag\n"; | |
} else { | |
$jmbag .= $sum; | |
} | |
return $jmbag; | |
} | |
print jmbag_check($_) . "\n" foreach @ARGV; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment