python-barcode icon indicating copy to clipboard operation
python-barcode copied to clipboard

Wrong checksum for ISSN

Open mezzelfo opened this issue 1 year ago • 0 comments

I think the current implementation of _calculate_checksum in InternationalStandardSerialNumber is wrong. Indeed, the tmp variable sometimes assume the value of "11", while should probably be "0", making the checksum two digits long!

This is a minimum replicating example:

from barcode import ISSN
print(ISSN("6727893").issn) # output = 672789311 when should be 67278930

Simply adding and extra %11 operation at the end of the tmp computation should solve the issue:

def _calculate_checksum(self):
    tmp = (
        11
        - sum(x * int(y) for x, y in enumerate(reversed(self.issn[:7]), start=2))
        % 11
    ) % 11
    if tmp == 10:
        return "X"

    return tmp

For reference:

https://github.com/WhyNotHugo/python-barcode/blob/389dc5fac558c89f189cef850febf56209f72b9d/barcode/isxn.py#L110

mezzelfo avatar Nov 27 '24 19:11 mezzelfo