views:

163

answers:

1

Does anyone know the formal / official name of the checksum algorithm used in the following functions?


function Checksum($number, $encode = true)
{
    if ($encode === true)
    {
        $result = 0;
        $number = str_split($number, 1);

        foreach ($number as $value)
        {
            $result = ($result + ord($value) - 48) * 10 % 97;
        }

        return implode('', $number) . sprintf('%02u', (98 - $result * 10 % 97) % 97);
    }

    else if ($number === Checksum(substr($number, 0, -2), true))
    {
        return substr($number, 0, -2);
    }

    return false;
}

function ifMB($entity, $reference, $amount = 0.00)
{
    $stack = 923;
    $weights = array(62, 45, 53, 15, 50, 5, 49, 34, 81, 76, 27, 90, 9, 30, 3);
    $argument = str_split(sprintf('%03u%04u%08u', $entity, $reference, round($amount * 100)), 1);

    foreach ($argument as $key => $value)
    {
        $stack += $value * $weights[$key];
    }

    return sprintf('%03u%04u%02u', $entity, $reference, 98 - ($stack % 97));
}

Thanks!

+4  A: 

It's an error correcting code described in ISO7604, designed to detect various common kinds of mistakes people make when transcribing digit strings:

  • Getting a single digit wrong
  • Transposing two digits (e.g., 1324 instead of 1234)
  • Adding or missing out a digit

The algorithm is mostly used in bank account number verification, but you can use it for pretty much any string of digits.

References:

Grandpa
I've seen this algorithm being used in several different places and none of them are related to IBANs. I've added another example to my question.
Alix Axel
OK, fixed up the answer a bit.
Grandpa