Skip to content

Instantly share code, notes, and snippets.

@eloonstra
Created March 6, 2025 09:07
Simple IBAN (checksum) validation in Go
package iban
import (
"math/big"
"strconv"
)
// IsValid ensures bank account numbers are valid to prevent processing errors
// in financial transactions and comply with international banking standards.
// It implements the ISO 13616 standard for IBAN validation, verifying the
// checksum using the MOD97 algorithm to catch transcription errors.
func IsValid(iban string) bool {
// Most IBANs should be between 15-34 characters (varies by country)
if len(iban) < 15 || len(iban) > 34 {
return false
}
// Apply the MOD97 algorithm required by ISO 13616:
// 1. Move country code and check digits to end to ensure uniform processing
rearranged := iban[4:] + iban[:4]
// 2. Convert letters to numbers per ISO standard to create a single large integer
result := ""
for _, character := range rearranged {
if character >= 'A' && character <= 'Z' {
// Convert letter to number (A=10, B=11, ..., Z=35)
result += strconv.Itoa(int(character-'A') + 10)
} else if character >= '0' && character <= '9' {
result += string(character)
} else {
// Invalid character
return false
}
}
// 3. Calculate modulo 97 - valid IBANs must give remainder of 1
// Using big.Int because IBAN numbers converted to integers exceed standard int sizes
num := new(big.Int)
num.SetString(result, 10)
mod := new(big.Int)
mod.Mod(num, big.NewInt(97))
return mod.Int64() == 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment