Luhn algorithm for check digit

Create check digits to barcodes with a simple formula!

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.

From Wikipedia

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.

Example

(00)15649794002003404

SSCC156497940020034044
X2 every other11068914980020064044
Sum digits7109875987020064044
  • 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!

November 22, 2019

0 responses on "Luhn algorithm for check digit"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Σχεδιασμός © GetCert. Με επιφύλαξη παντός δικαιώματος.