This is an article that refers to Luhn algorithm for creating a check digit. Check digits are found in Barcodes like SSCC (Serial Shipping Container Code) or EAN13, GS128, etc.
The Luhn algorithm or Luhn formula, also known as the “modulus 10” or “mod 10” algorithm, named after its creator, IBM scientist Hans Peter Luhn, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers, Greek Social Security Numbers (ΑΜΚΑ), and survey codes appearing on McDonald’s, Taco Bell, and Tractor Supply Co. receipts. It is described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.
Let’s take the example of SSCC barcode
SSCC barcode have application identifier (00) and after that they contain 17 digits +1 which is check digit that is calculated from the Luhn algorithm.
From the rightmost digit (excluding the check digit) and moving left, double the value of every second digit. The check digit is neither doubled nor included in this calculation; the first digit doubled is the digit located immediately left of the check digit.
If the result of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then add the digits of the result (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9) or, alternatively, the same final result can be found by subtracting 9 from that result (e.g., 16: 16 − 9 = 7, 18: 18 − 9 = 9).
Take the sum of all the digits.
If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; otherwise it is not valid.
|X2 every other||1||10||6||8||9||14||9||8||0||0||2||0||0||6||4||0||4||4|
- After that we take the sum the third row (Sum digits) which is 7+10+9+8+7+5+9+8+7+0+2+0+0+6+4+0+4 = 86
- multiply 86 by 9 = 774
- 774 MOD 10 = 4
Check digit is 4
Thanks to luhn’s algorithm we can get any check digit we want. Drop me a comment if you want to ask something!