1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
package iban
import (
"bytes"
"fmt"
"math/big"
"strings"
)
const countryDE = "DE"
const germanAccountIDLength = 10
const maxAllowedIBANLength = 34
// IsValid validates the IBAN for its proof number
func IsValid(input string) bool {
iban, err := From(input)
if err != nil {
return false
}
return iban.Valid()
}
// NewGerman calculates the Iban for the provided bankID and accountID.
// It will return an error if the accountID can not be parsed as int.
// It returns only valid german IBANs, as it is hard coded to use german
// settings.
func NewGerman(bankID, accountID string) (IBAN, error) {
if len(accountID) == (germanAccountIDLength - 1) {
accountID = "0" + accountID
}
bban := fmt.Sprintf("%s%s", bankID, accountID)
return New(countryDE, bban)
}
// New returns a new IBAN for the provided countryCode and BBAN
func New(countryCode string, BBAN string) (IBAN, error) {
if len(countryCode) != 2 {
return "", fmt.Errorf("malformed countryCode: must have two characters")
}
countryCode = strings.ToUpper(countryCode)
ibanLen, ok := ibanMaxLengthForCountry[countryCode]
if !ok {
ibanLen = maxAllowedIBANLength
}
if len(BBAN) == 0 {
return "", fmt.Errorf("missing BBAN")
}
if len(BBAN) > (ibanLen - 4) {
return "", fmt.Errorf("malformed BBAN: must have at max %d characters", ibanLen)
}
BBAN = strings.ToUpper(BBAN)
step1 := fmt.Sprintf("%s%s00", transformLettersToDigits(BBAN), transformLettersToDigits(countryCode))
step2, ok := new(big.Int).SetString(step1, 10)
if !ok {
return "", fmt.Errorf("Malformed iban string: %s", step1)
}
step3 := step2.Mod(step2, big.NewInt(97))
proofNumber := new(big.Int).Sub(big.NewInt(98), step3)
iban := fmt.Sprintf("%s%02d%s", countryCode, proofNumber.Int64(), BBAN)
return IBAN(iban), nil
}
// From creates an IBAN from the provided input. If the input is not a valid IBAN, it
// returns an error.
func From(input string) (IBAN, error) {
countryCode := input[:2]
proofNumber := input[2:4]
bban := input[4:]
iban, err := New(countryCode, bban)
if err != nil {
return "", err
}
if proofNumber != iban.ProofNumber() {
return "", fmt.Errorf("proof number invalid")
}
return iban, nil
}
// IBAN represents an International Bank Account Number.
// It is defined by ISO 13616:2007
type IBAN string
// BBAN returns the BBAN, that is, the bank account identifier
func (i IBAN) BBAN() string {
return string(i[4:])
}
// BankID returns the parts of the IBAN which refer to the bank institute ID
func (i IBAN) BankID() string {
return string(i[4:12])
}
// AccountID returns the parts of the IBAN which refer to the AccountID
func (i IBAN) AccountID() string {
accountID := string(i[12:])
return strings.TrimLeft(accountID, "0")
}
// CountryCode returns the country code used by the IBAN.
// The country code is defined by ISO 3166-1 alpha-2
func (i IBAN) CountryCode() string {
return string(i[:2])
}
// ProofNumber returns the used number to make a sanity check whether the IBAN is valid or not.
func (i IBAN) ProofNumber() string {
return string(i[2:4])
}
// Valid returns true if the IBAN is valid, false otherwise
func (i IBAN) Valid() bool {
if len(i) > maxAllowedIBANLength {
return false
}
countryCode := i.CountryCode()
countryCode = strings.ToUpper(countryCode)
bban := i.BBAN()
bban = strings.ToUpper(bban)
digitCountryCode := transformLettersToDigits(countryCode)
digitBBAN := transformLettersToDigits(bban)
checkSumString := fmt.Sprintf("%s%s%s", digitBBAN, digitCountryCode, i.ProofNumber())
checkSum, ok := new(big.Int).SetString(checkSumString, 10)
if !ok {
return false
}
mod := checkSum.Mod(checkSum, big.NewInt(97))
return mod.Cmp(big.NewInt(1)) == 0
}
// String returns the string representation of i
func (i IBAN) String() string {
return string(i)
}
// Print returns the IBAN in paper format, i.e. with spaces after every fourth
// character
func Print(iban IBAN) string {
ibanStr := iban.String()
for len(ibanStr)%4 != 0 {
ibanStr += " "
}
var out bytes.Buffer
for i := 4; i <= len(ibanStr); i += 4 {
out.WriteString(ibanStr[i-4 : i])
out.WriteString(" ")
}
return strings.TrimSpace(out.String())
}
func transformLettersToDigits(letters string) string {
var replaced []string
for k, v := range alphaToDigit {
replaced = append(replaced, k)
replaced = append(replaced, v)
}
return strings.NewReplacer(replaced...).Replace(letters)
}
var alphaToDigit = map[string]string{
"A": "10",
"B": "11",
"C": "12",
"D": "13",
"E": "14",
"F": "15",
"G": "16",
"H": "17",
"I": "18",
"J": "19",
"K": "20",
"L": "21",
"M": "22",
"N": "23",
"O": "24",
"P": "25",
"Q": "26",
"R": "27",
"S": "28",
"T": "29",
"U": "30",
"V": "31",
"W": "32",
"X": "33",
"Y": "34",
"Z": "35",
}
var ibanMaxLengthForCountry = map[string]int{
"AL": 28,
"AD": 24,
"AZ": 28,
"BH": 22,
"BE": 16,
"BA": 20,
"BR": 29,
"VG": 24,
"BG": 22,
"CR": 22,
"DK": 18,
"DE": 22,
"DO": 28,
"SV": 28,
"EE": 20,
"FO": 18,
"FI": 18,
"FR": 27,
"GE": 22,
"GI": 23,
"GR": 27,
"GL": 18,
"GB": 22,
"GT": 28,
"IQ": 23,
"IE": 22,
"IS": 26,
"IL": 23,
"IT": 27,
"JO": 30,
"KZ": 20,
"QA": 29,
"XK": 20,
"HR": 21,
"KW": 30,
"LV": 21,
"LB": 28,
"LI": 21,
"LT": 20,
"LU": 20,
"MT": 31,
"MR": 27,
"MU": 30,
"MK": 19,
"MD": 24,
"MC": 27,
"ME": 22,
"NL": 18,
"NO": 15,
"AT": 20,
"PK": 24,
"PS": 29,
"PL": 28,
"PT": 25,
"RO": 24,
"LC": 32,
"SM": 27,
"ST": 25,
"SA": 24,
"SE": 24,
"CH": 21,
"RS": 22,
"SC": 31,
"SK": 24,
"SI": 19,
"ES": 24,
"TL": 23,
"TR": 26,
"CZ": 24,
"TN": 24,
"UA": 29,
"HU": 28,
"AE": 23,
"BY": 28,
"CY": 28,
}
var sepaSupportForCountry = map[string]bool{
"AL": false,
"AD": false,
"AZ": false,
"BH": false,
"BE": true,
"BA": false,
"BR": false,
"VG": false,
"BG": true,
"CR": false,
"DK": true,
"DE": true,
"DO": false,
"SV": false,
"EE": true,
"FO": true,
"FI": true,
"FR": true,
"GE": false,
"GI": true,
"GR": true,
"GL": true,
"GB": true,
"GT": false,
"IQ": false,
"IE": true,
"IS": true,
"IL": false,
"IT": true,
"JO": false,
"KZ": false,
"QA": false,
"XK": false,
"HR": true,
"KW": false,
"LV": true,
"LB": false,
"LI": true,
"LT": true,
"LU": true,
"MT": true,
"MR": false,
"MU": false,
"MK": false,
"MD": false,
"MC": true,
"ME": false,
"NL": true,
"NO": true,
"AT": true,
"PK": false,
"PS": false,
"PL": true,
"PT": true,
"RO": true,
"LC": false,
"SM": true,
"ST": false,
"SA": false,
"SE": true,
"CH": true,
"RS": false,
"SC": false,
"SK": true,
"SI": true,
"ES": true,
"TL": false,
"TR": false,
"CZ": true,
"TN": false,
"UA": false,
"HU": true,
"AE": false,
"BY": false,
"CY": true,
}
|