Informal explanation
The Luhn mod N algorithm generates a check digit (more precisely, a check character) within the same range of valid characters as the input string. For example, if the algorithm is applied to a string of lower-case letters (''a'' to ''z''), the check character will also be a lower-case letter. Apart from this distinction, it resembles very closely the original algorithm. The main idea behind the extension is that the full set of valid input characters is mapped to a list of code-points (i.e., sequential integers beginning with zero). The algorithm processes the input string by converting each character to its associated code-point and then performing the computations in mod N (where is the number of valid input characters). Finally, the resulting check code-point is mapped back to obtain its corresponding check character.Limitation
The Luhn mod N algorithm only works where is divisable by 2. This is because there is an operation to correct the value of a position after doubling its value which does not work where is not divisable by 2. For applications using theExplanation
The second step in the Luhn algorithm re-packs the doubled value of a position into the original digit's base by adding together the individual digits in the doubled value when written in base . This step results in even numbers if the doubled value is less than or equal to , and odd numbers if the doubled value is greater than . For example, in Decimal applications where is 10, original values between 0 and 4 result in even numbers and original values between 5 and 9 result in odd numbers, effectively re-packing the doubled values between 0 and 18 into a single distinct result between 0 and 9. Where an is used that is not divisable by 2 this step returns even numbers for doubled values greater than which cannot be distinguished from doubled values less than or equal to .Outcome
The algorithm will neither detect all single-digit errors nor all transpositions of adjacent digits if an is used that is not divisable by 2. As these detection capabilities are the algorithm's primary strengths, the algorithm is weakened almost entirely by this limitation. The Luhn mod N algorithm odd variation enables applications where is not divisable by 2 by replacing the doubled value at each position with the remainder after dividing the position's value by which gives odd number remainders consistent with the original algorithm design.Mapping characters to code-points
Initially, a mapping between valid input characters and code-points must be created. For example, consider that the valid characters are the lower-case letters from ''a'' to ''f''. Therefore, a suitable mapping would be: Note that the order of the characters is completely irrelevant. This other mapping would also be acceptable (although possibly more cumbersome to implement): It is also possible to intermix letters and digits (and possibly even other characters). For example, this mapping would be appropriate for lower-case hexadecimal digits:Algorithm in C#
Assuming the following functions are defined:Algorithm in Java
Assuming the following functions are defined:Algorithm in JavaScript
Assuming the following functions are defined:Example
Generation
Consider the above set of valid input characters and the example input string . To generate the check character, start with the last character in the string and move left doubling every other code-point. The "digits" of the code-points as written in base 6 (since there are 6 valid input characters) should then be summed up: The total sum of digits is 14 (0 + 2 + 2 + 1 + 4 + 5). The number that must be added to obtain the next multiple of 6 (in this case, 18) is 4. This is the resulting check code-point. The associated check character is e.Validation
The resulting string can then be validated by using a similar procedure: {, class="wikitable" style="width:500px;" , -align=center ! Character , ''a'' , , ''b'' , , ''c'' , , ''d'' , , ''e'' , , ''f'' , , ''e'' , -align=center ! Code-point , 0 , , 1 , , 2 , , 3 , , 4 , , 5 , , 4 , -align=center ! Double , , , 2 , , , , 6 (base 10)Implementation
The mapping of characters to code-points and back can be implemented in a number of ways. The simplest approach (akin to the original Luhn algorithm) is to use ASCII code arithmetic. For example, given an input set of ''0'' to ''9'', the code-point can be calculated by subtracting the ASCII code for '0' from the ASCII code of the desired character. The reverse operation will provide the reverse mapping. Additional ranges of characters can be dealt with by using conditional statements. Non-sequential sets can be mapped both ways using a hard-coded ''switch/case'' statement. A more flexible approach is to use something similar to anWeakness
This extension shares the same weakness as the original algorithm, namely, it cannot detect the transposition of the sequence ''See also
* International Securities Identification Number (ISIN) Modular arithmetic Checksum algorithms Articles with example code Articles with example C Sharp code