-
Notifications
You must be signed in to change notification settings - Fork 132
Description
Hi there, thank you for making this library!
I have an issue with TD1, specifically scanning Belgian ID cards. If the document_number_hash digit is "<" the document will not verify.
I have checked this with 3 different Belgian ID cards and they all have "<" on index 14 of line 0.
After a ton of googling and reading specs I found an issue with the way you check document_number_hash...
Normally a document number starts at position 5 and ends at position 13 but sometimes a document number exceeds the size of it's slot and optional fields will be used, let's take a look at this example:
IDBEL123456789<1233<<<<<<<<<<<
In this case the document number check is < when we have a scenario like that we need to look at the optional numbers (1233). So when the document number check is < we need to look at the last none empty value: 3. This is the actual hash number. After that we simply verify the hash of:
Document Number: 123456789<123
Hash: 3
And this should verify as True using your verify function.
from string import ascii_uppercase, digits
def hash_string(string: str) -> str:
"""
>>> hash_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
'7'
>>> hash_string("0123456789")
'7'
>>> hash_string("0123456789ABCDEF")
'0'
>>> hash_string("0")
'0'
"""
printable = digits + ascii_uppercase
string = string.upper().replace("<", "0")
weight = [7, 3, 1]
summation = 0
for i in range(len(string)):
c = string[i]
if c not in printable:
raise ValueError("%s contains invalid characters" % string, c)
summation += printable.index(c) * weight[i % 3]
return str(summation % 10)
print(hash_string("123456789<123"))