Last active
January 15, 2019 10:13
-
-
Save richarddewit/91a440b79cce9f46254789f1a0ae6852 to your computer and use it in GitHub Desktop.
Generate X random IBANs for Dutch bank ABN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import random | |
def main(): | |
try: | |
count = int(sys.argv[1]) | |
if count <= 0: | |
raise ValueError | |
except (IndexError, ValueError): | |
count = 1 | |
ibans = get_random_ibans(count) | |
for iban in ibans: | |
print iban | |
print '' | |
iban_count = len(ibans) | |
print iban_count, 'ibans' if iban_count != 1 else 'iban' | |
def get_random_ibans(count=10, ibans=[]): | |
iban = try_iban() | |
if not iban in ibans: | |
ibans.append(iban) | |
if len(ibans) == count: | |
return ibans | |
return get_random_ibans(count, ibans) | |
def try_iban(): | |
A = get_letter_number('A') | |
B = get_letter_number('B') | |
L = get_letter_number('L') | |
N = get_letter_number('N') | |
rand = random.randint(1, 999999999) | |
sequence = int(list_to_string([ | |
A, B, N, A, | |
format(rand, '010'), | |
N, L, | |
'00', | |
])) | |
control_number = 98 - (sequence % 97) | |
return list_to_string([ | |
'NL', | |
format(control_number, '02'), | |
'ABNA', | |
format(rand, '010'), | |
]) | |
def get_letter_number(letter): | |
number = ord(letter) - 55 | |
return format(number, '02') | |
def list_to_string(ls=[]): | |
return ''.join(map(str, ls)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment