Skip to content

Commit

Permalink
Creating French BBAN (RIB) checksum algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremi-streammind committed Nov 29, 2021
1 parent e9fdf87 commit 1816a01
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions schwifty/checksum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ def register(algorithm_cls: Type[Algorithm], prefix: Optional[str] = None) -> Ty
from schwifty.checksum import belgium # noqa
from schwifty.checksum import germany # noqa
from schwifty.checksum import italy # noqa
from schwifty.checksum import france # noqa
45 changes: 45 additions & 0 deletions schwifty/checksum/france.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import functools

from schwifty import checksum
from schwifty import registry


register = functools.partial(checksum.register, prefix="FR")

CONVERT_LETTERS = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'A': '1', 'B': '2', 'C': '3', 'D': '4', 'E': '5', 'F': '6', 'G': '7', 'H': '8', 'I': '9',
'J': '1', 'K': '2', 'L': '3', 'M': '4', 'N': '5', 'O': '6', 'P': '7', 'Q': '8', 'R': '9',
'S': '2', 'T': '3', 'U': '4', 'V': '5', 'W': '6', 'X': '7', 'Y': '8', 'Z': '9',
}

@register
class DefaultAlgorithm(checksum.Algorithm):
name = "default"
accepts = checksum.InputType.BBAN
checksum_length = 2

def compute(self, bban: str) -> str:
spec = registry.get("iban")
assert isinstance(spec, dict)
bank_code = bban[spec["FR"]["positions"]["bank_code"][0]:spec["FR"]["positions"]["bank_code"][1]]
branch_code = bban[spec["FR"]["positions"]["branch_code"][0]:spec["FR"]["positions"]["branch_code"][1]]
account_code = bban[spec["FR"]["positions"]["account_code"][0]:spec["FR"]["positions"]["account_code"][1]]

num_bank_code = ''
for bank_code_char in bank_code:
num_bank_code = num_bank_code + CONVERT_LETTERS[bank_code_char]

num_branch_code = ''
for branch_code_char in branch_code:
num_branch_code = num_branch_code + CONVERT_LETTERS[branch_code_char]

num_account_code = ''
for account_code_char in account_code:
num_account_code = num_account_code + CONVERT_LETTERS[account_code_char]

checksum = 97 -((89 * int(num_bank_code) + 15 * int(num_branch_code) + 3 * int(num_account_code)) % 97)
return str(checksum).zfill(self.checksum_length)

def validate(self, bban: str) -> bool:
return bban[-self.checksum_length :] == self.compute(bban[: -self.checksum_length])
3 changes: 3 additions & 0 deletions schwifty/iban.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def add_bban_checksum(country_code: str, bban: str) -> str:
bban = bban[:3] + bban[5:]
checksum = algorithms["BE:default"].compute(bban)
bban = bban + checksum
elif country_code == "FR":
checksum = algorithms["FR:default"].compute(bban)
bban = bban[0:21] + checksum
return bban


Expand Down
8 changes: 4 additions & 4 deletions schwifty/iban_registry/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,16 @@
"iban_length": 27,
"positions": {
"account_code": [
5,
23
10,
21
],
"bank_code": [
0,
5
],
"branch_code": [
5,
5
10
]
}
},
Expand Down Expand Up @@ -1899,4 +1899,4 @@
]
}
}
}
}

0 comments on commit 1816a01

Please sign in to comment.