diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2017-04-10 21:58:01 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2017-04-10 22:01:33 +0200 |
commit | e844b5235a1a0599cabe6a5f1b27228b3036db05 (patch) | |
tree | ce6d69c066310523cb0c0e4507ee53f8b140434b | |
parent | 1b3d16ee525ae941d49be8a27acca66b0e73c44d (diff) |
Integrate base10 conversion into Mod 97, 10
This moves the conversion of an alphanumeric string to a numeric
representation for modulo 97 calculation to the mod_97_10 module because
this mechanism seems to be used by multiple formats.
-rw-r--r-- | stdnum/iban.py | 25 | ||||
-rw-r--r-- | stdnum/iso7064/mod_97_10.py | 17 |
2 files changed, 19 insertions, 23 deletions
diff --git a/stdnum/iban.py b/stdnum/iban.py index 18a9faa..a9c56b3 100644 --- a/stdnum/iban.py +++ b/stdnum/iban.py @@ -1,6 +1,6 @@ # iban.py - functions for handling International Bank Account Numbers (IBANs) # -# Copyright (C) 2011-2016 Arthur de Jong +# Copyright (C) 2011-2017 Arthur de Jong # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -55,9 +55,6 @@ from stdnum.util import clean, get_cc_module # our open copy of the IBAN database _ibandb = numdb.get('iban') -# the valid characters we have -_alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' - # regular expression to check IBAN structure _struct_re = re.compile(r'([1-9][0-9]*)!([nac])') @@ -71,25 +68,11 @@ def compact(number): return clean(number, ' -').strip().upper() -def _to_base10(number): - """Prepare the number to its base10 representation (also moving the - check digits to the end) so it can be checked with the ISO 7064 - Mod 97, 10 algorithm.""" - # TODO: find out whether this should be in the mod_97_10 module - try: - return ''.join( - str(_alphabet.index(x)) for x in number[4:] + number[:4]) - except Exception: - raise InvalidFormat() - - def calc_check_digits(number): """Calculate the check digits that should be put in the number to make - it valid. Check digits in the supplied number are ignored..""" + it valid. Check digits in the supplied number are ignored.""" number = compact(number) - # replace check digits with placeholders - number = ''.join((number[:2], '00', number[4:])) - return mod_97_10.calc_check_digits(_to_base10(number)[:-2]) + return mod_97_10.calc_check_digits(number[4:] + number[:2]) def _struct_to_re(structure): @@ -119,7 +102,7 @@ def validate(number, check_country=True): specific check can be disabled with the check_country argument.""" number = compact(number) # ensure that checksum is valid - mod_97_10.validate(_to_base10(number)) + mod_97_10.validate(number[4:] + number[:4]) # look up the number info = _ibandb.info(number) # check if the bban part of number has the correct structure diff --git a/stdnum/iso7064/mod_97_10.py b/stdnum/iso7064/mod_97_10.py index 618fbc7..39f05c4 100644 --- a/stdnum/iso7064/mod_97_10.py +++ b/stdnum/iso7064/mod_97_10.py @@ -1,6 +1,6 @@ # mod_97_10.py - functions for performing the ISO 7064 Mod 97, 10 algorithm # -# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong +# Copyright (C) 2010-2017 Arthur de Jong # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -37,9 +37,22 @@ valid if the number modulo 97 is 1. As such it has two check digits. from stdnum.exceptions import * +# the valid characters we have +_alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' + + +def _to_base10(number): + """Prepare the number to its base10 representation.""" + try: + return ''.join( + str(_alphabet.index(x)) for x in number) + except Exception: + raise InvalidFormat() + + def checksum(number): """Calculate the checksum. A valid number should have a checksum of 1.""" - return int(number) % 97 + return int(_to_base10(number)) % 97 def calc_check_digits(number): |