我想借助 OCR 读取大约 16-20 个字符(AZ,0-9)的序列号。因为每次我想在序列号中添加一个校验字符时,所有字符都不会被正确识别。目前我找到了简单的 Luhn mod N 算法(维基百科)。该算法对于转置错误 (09 => 90) 是不安全的。
来自维基百科的实现:
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() 将是 36 (AZ, 0-9)
但是,如果我将“因子”变量修改为序列号中字符的实际索引,是否会像以前一样更安全?(参见代码中的 ** ** 行)