I want to read a serial number of about 16-20 characters (A-Z, 0-9) with the help of OCR. Since all character won't be recognized correctly every time I want to add one check character to the serial number. At the moment I found the simple Luhn mod N algo (Wikipedia). This algorithm isn't safe about transposition errors (09 => 90).
Implementation from Wikipedia:
char GenerateCheckCharacter(string input) {
int factor = 2;
int sum = 0;
int n = NumberOfValidInputCharacters();
// Starting from the right and working leftwards is easier since
// the initial "factor" will always be "2"
**//int index = 0;**
for (int i = input.Length - 1; i >= 0; i--) {
int codePoint = CodePointFromCharacter(input[i]);
int addend = factor * codePoint;
// Alternate the "factor" that each "codePoint" is multiplied by
factor = (factor == 2) ? 1 : 2;
**//factor = index;**
// Sum the digits of the "addend" as expressed in base "n"
addend = (addend / n) + (addend % n);
sum += addend;
**//index++;**
}
// Calculate the number that must be added to the "sum"
// to make it divisible by "n"
int remainder = sum % n;
int checkCodePoint = (n - remainder) % n;
return CharacterFromCodePoint(checkCodePoint);
}
NumberOfValidInputCharacters() would be 36 (A-Z, 0-9)
But if I modify the "factor" variable to the actual index of the character inside the serial number, is it then safer as before? (see ** ** lines in the code)