-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibancheck.py
41 lines (34 loc) · 1.3 KB
/
ibancheck.py
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
import string
class IBANcheck:
@staticmethod
def check(iban: str) -> bool:
(countrycode, checksum, bban) = IBANcheck.splitiban(iban)
countrycode_num = IBANcheck.string2numstring(countrycode)
checksum_num = IBANcheck.string2numstring(checksum)
bban_num = IBANcheck.string2numstring(bban)
str_under_check = bban_num + countrycode_num + checksum_num
return int(str_under_check) % 97 == 1
@staticmethod
def splitiban(iban: str) -> (str, str, str):
strippediban = "".join(iban.split()) #this kills all whitespaces
countrycode = strippediban[0:2] #"DE"
checksum = strippediban[2:4] #"12"
bban = strippediban[4:] #"3456789"
return (countrycode, checksum, bban)
@staticmethod
def letter2num(letter: str) -> str:
numbers = list(string.digits)
letters = list(string.ascii_uppercase)
lookup = numbers + letters
try:
position = str(lookup.index(letter))
except ValueError:
position = ""
return position
@staticmethod
def string2numstring(input:str) -> str:
result = []
for char in input:
num4letter = IBANcheck.letter2num(char)
result.append(num4letter)
return ''.join(result)