Skip to content
/ Iban Public

Implementation of Modulo 97 algorithm to verify check digits fast and with constant memory.

Notifications You must be signed in to change notification settings

joacar/Iban

Repository files navigation

IBAN

An implementation of the modulo 97 algorithm for calculating checksum avoiding use of large integers and allow operation in unsigned integers (< 2^31). Properties of congruence relations, more specifically this, give the correctnes of algorithm.

Background

The project started to become acquainted with Span<T> to reduce costly string manipulations when calculating the checksum. Along the way I realized that it's possible without any allocations, so the use of ReadOnlySpan<char> in this case could just as well be replaced with char[]. The iterator IbanDigitizer could then strip the ref keyword which would allow to implement IReadOnlyCollection<int> and make for an even neater API.

Example

Lets take iban number for a Croatian bank and disect the steps.

  1. Move last four to the end

    CR23015108410023612345 -> 015108410023612345CR23

  2. Replace characters with digits

    015108410023612345CR23 -> 015108410023612345122723

  3. Start iteration over nine digit numbers

    1. N = 0151084410, d = N % 97 = 78
    2. N = _78_0260123, d = N % 97 = 77
    3. N = _77_451227, d = N % 97 = 25
    4. N = _25_23, d = N % 97 = 1
  4. If d == 1 then valid; otherwise invalid.

The implemention make us of a specialized "iterator" that perform step 1 and 2 without any allocations.

About

Implementation of Modulo 97 algorithm to verify check digits fast and with constant memory.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages