-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathibantools.py
More file actions
68 lines (57 loc) · 1.83 KB
/
ibantools.py
File metadata and controls
68 lines (57 loc) · 1.83 KB
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
import re
# IBAN Prüfzifferberechnung nach http://www.pruefziffernberechnung.de/Originaldokumente/IBAN/Prufziffer_07.00.pdf
def __hilfsmethode__(numiban):
# 1. Teilschritt
irest=numiban
while irest:
i=numiban[:9]
rs=str(int(i)%97)
irest=numiban[9:]
numiban=rs+irest
return int(rs)
def validiere(iban):
# Schritt 1: IBAN und nicht-alpha-Zeichen entfernen
iban = re.sub('^IBAN', '', iban)
iban = re.sub('[^\w]', '', iban)
# Schritt 2: Erste vier Zeichen an Ende Schieben
iban = iban[4:] + iban[:4]
# Schritt 3: Alphazeichen umwandeln
iban2=''
for char in iban:
if char >= 'A':
iban2 += str(ord(char) - ord('A') + 10)
else:
iban2 += char
iban = iban2
# Schritt 4: Durch 97 teilen
# Da die Nummer u.U. zu gross ist, wird das iterative Verfahren verwendet
r=__hilfsmethode__(iban)
return(r == 1)
def generiere(land = 'CH', bc = 1, kontonummer = 'A123456789'):
# Schritt 1
*iban_chars, = land + '00' + "{:0>5}".format(bc) + "{:0>12}".format(kontonummer)
# Schritt 2
iban_chars = iban_chars[4:] + iban_chars[:4]
# Schritt 3
i = 0
while i < len(iban_chars):
if iban_chars[i] >= 'A':
iban_chars[i] = str(ord(iban_chars[i]) - ord('A') + 10)
i+=1
# Scritt 4
r = __hilfsmethode__(str(''.join(iban_chars)))
r = 98 - r
iban_chars[-2]=str(r//10)
iban_chars[-1]=str(r%10)
# Schritt 5
iban_chars = iban_chars[-4:] + iban_chars[:-4]
# Schritt 6
i=0
while i < len(iban_chars):
if int(iban_chars[i]) > 9:
iban_chars[i] = chr(int(iban_chars[i]) + ord('A') - 10)
i+=1
iban = "".join(iban_chars)
return iban
if __name__ == "__main__":
print("Dies ist ein Modul und sollte eingebunden werden")