ISO 7064 is a standard promulgated by the International Standards Organization that defines algorithms for calculating check digit characters. The checks should be applicable to alphanumeric strings and should be able to detect all single substitution errors, all or nearly all single local transposition errors, all or nearly all circular shift errors, a high proportion of double substitution errors, a high proportion of all other errors.
It is used in some other ISO standards like IBAN, ISTC, ISNI, LEI. In our everyday life we see it in IBAN, but also we can use it many other systems like identification systems (Croatia and China) and other.
So, I personally needed a library to verify and calculate those check digits in Go. After some research I’ve found out that there are a few libraries like digitorus/iso7064 and almerlucke/go-iban, but they are hardcoded for MOD97–10 (used in IBAN) and that is all. Other algorithms like MOD11–2, MOD37–2, MOD661–26 and MOD1271–36 are not implemented in them. So, I’ve decided to create a library which will fill my needs. First of all, implementation of all algorithms listed above.
A library which will allow to calculate check digit characters according to ISO7064 using MOD11–2, MOD37–2, MOD97–10, MOD661–26, MOD1271–36 and also will allow to easily calculate and embed check digit characters for IBAN.
Everything is based on interface called Calculator, which describes methods to verify, compute full string and compute check digit characters. You can see it’s code below:
Then there is a structure called BaseCalculator, which implements methods from Calculator interface implements ISO7064 base algorithm. It’s code is:
As you can see BaseCalculator structure has fields called Modulus, Radix, Charset and IsDouble. So, various combinations of these fields allow us to create different algorithms described in ISO7064 like MOD11–2, MOD37–2, MOD97–10, MOD661–26, MOD1271–36, but also use any other combination to create another nonstandard algorithm based on ISO7064 base algorithm. There is an example how to create BaseCalculator for ISO7064 MOD97–10:
So, by analogy, we can create BaseCalculators for other algorithms.
But also we should implement IBANCalculator which will do IBAN specific steps like rearranging and letter to integer conversion. There is it’s code:
It uses BaseController with MOD97–10 configuration to compute check digit characters and also rearranges and converts letter to integers in IBAN before the calculation.
I’ve never written any articles on Medium or anywhere else, so thanks for until the end. The library itself is available on GitHub pkasila/iso7064 under Apache License 2.0, so you can try it out, I wrote some tests, and it seems to work fine. If you have some ideas you can share them in Discussions or in comments and if you have spotted a bug, then file an issue on GitHub. Also, you create a pull request yourself and make the library better.