Luhn algorithm

Last updated

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 check digit formula used to validate a variety of identification numbers. It is described in US patent 2950048A, granted on 23 August 1960. [1]

Contents

The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 7812-1. [2] It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.

Description

The check digit is computed as follows:

  1. Drop the check digit from the number (if it's already present). This leaves the payload.
  2. Start with the payload digits. Moving from right to left, double every second digit, starting from the last digit. If doubling a digit results in a value > 9, subtract 9 from it (or sum its digits).
  3. Sum all the resulting digits (including the ones that were not doubled).
  4. The check digit is calculated by , where s is the sum from step 3. This is the smallest number (possibly zero) that must be added to to make a multiple of 10. Other valid formulas giving the same value are , , and . Note that the formula will not work in all environments due to differences in how negative numbers are handled by the modulo operation.

Example for computing check digit

Assume an example of an account number 1789372997 (just the "payload", check digit not yet included):

7992739871
Multipliers2121212121
==========
149182143188141
Sum digits5 (1+4)99 (1+8)25 (1+4)39 (1+8)85 (1+4)1

The sum of the resulting digits is 56.

The check digit is equal to .

This makes the full account number read 17893729974.

Example for validating check digit

  1. Drop the check digit (last digit) of the number to validate. (e.g. 17893729974 1789372997)
  2. Calculate the check digit (see above)
  3. Compare your result with the original check digit. If both numbers match, the result is valid. (e.g. (givenCheckDigit = calculatedCheckDigit) (isValidCheckDigit)).

Strengths and weaknesses

The Luhn algorithm will detect all single-digit errors, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa). It will detect most of the possible twin errors (it will not detect 2255, 3366 or 4477).

Other, more complex check-digit algorithms (such as the Verhoeff algorithm and the Damm algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports non-numerical strings.

Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that pad to a specific number of digits (by converting 1234 to 0001234 for instance) can perform Luhn validation before or after the padding and achieve the same result.

The algorithm appeared in a United States Patent [1] for a simple, hand-held, mechanical device for computing the checksum. The device took the mod 10 sum by mechanical means. The substitution digits, that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.

Pseudocode implementation

The following function takes a card number, including the check digit, as an array of integers and outputs true if the check digit is correct, false otherwise.

function isValid(cardNumber[1..length])     sum := 0     parity := length mod 2     for i from 1 to length doif i mod 2 != parity then             sum := sum + cardNumber[i]         elseif cardNumber[i] > 4 then             sum := sum + 2 * cardNumber[i] - 9         else             sum := sum + 2 * cardNumber[i]         end ifend forreturn cardNumber[length] == ((10 - (sum mod 10)) mod 10) end function

Uses

The Luhn algorithm is used in a variety of systems, including:

Related Research Articles

<span class="mw-page-title-main">ISBN</span> Unique numeric book identifier since 1970

The International Standard Book Number (ISBN) is a numeric commercial book identifier that is intended to be unique. Publishers purchase or receive ISBNs from an affiliate of the International ISBN Agency.

An International Securities Identification Number (ISIN) is a code that uniquely identifies a security globally for the purposes of facilitating clearing, reporting and settlement of trades. Its structure is defined in ISO 6166. The ISIN code is a 12-character alphanumeric code that serves for uniform identification of a security through normalization of the assigned National Number, where one exists, at trading and settlement.

In recreational mathematics, a Keith number or repfigit number is a natural number in a given number base with digits such that when a sequence is created such that the first terms are the digits of and each subsequent term is the sum of the previous terms, is part of the sequence. Keith numbers were introduced by Mike Keith in 1987. They are computationally very challenging to find, with only about 100 known.

A check digit is a form of redundancy check used for error detection on identification numbers, such as bank account numbers, which are used in an application where they will at least sometimes be input manually. It is analogous to a binary parity bit used to check for errors in computer-generated data. It consists of one or more digits computed by an algorithm from the other digits in the sequence input.

The Trachtenberg system is a system of rapid mental calculation. The system consists of a number of readily memorized operations that allow one to perform arithmetic computations very quickly. It was developed by the Russian engineer Jakow Trachtenberg in order to keep his mind occupied while being in a Nazi concentration camp.

Casting out nines is any of three arithmetical procedures:

<span class="mw-page-title-main">Doomsday rule</span> Way of calculating the day of the week of a given date

The Doomsday rule, Doomsday algorithm or Doomsday method is an algorithm of determination of the day of the week for a given date. It provides a perpetual calendar because the Gregorian calendar moves in cycles of 400 years. The algorithm for mental calculation was devised by John Conway in 1973, drawing inspiration from Lewis Carroll's perpetual calendar algorithm. It takes advantage of each year having a certain day of the week upon which certain easy-to-remember dates, called the doomsdays, fall; for example, the last day of February, April 4 (4/4), June 6 (6/6), August 8 (8/8), October 10 (10/10), and December 12 (12/12) all occur on the same day of the week in any year.

A divisibility rule is a shorthand and useful way of determining whether a given integer is divisible by a fixed divisor without performing the division, usually by examining its digits. Although there are divisibility tests for numbers in any radix, or base, and they are all different, this article presents rules and examples only for decimal, or base 10, numbers. Martin Gardner explained and popularized these rules in his September 1962 "Mathematical Games" column in Scientific American.

ISO/IEC 7812Identification cards – Identification of issuers is an international standard published jointly by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC). It specifies "a numbering system for the identification of the card issuers, the format of the issuer identification number (IIN) and the primary account number (PAN)", and procedures for registering IINs. It was first published in 1989.

The International Standard Musical Work Code (ISWC) is a unique identifier for musical works, similar to ISBN for books. It is adopted as international standard ISO 15707. The ISO subcommittee with responsibility for the standard is TC 46/SC 9.

Zeller's congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian day and the calendar date.

A spigot algorithm is an algorithm for computing the value of a transcendental number that generates the digits of the number sequentially from left to right providing increasing precision as the algorithm proceeds. Spigot algorithms also aim to minimize the amount of intermediate storage required. The name comes from the sense of the word "spigot" for a tap or valve controlling the flow of a liquid. Spigot algorithms can be contrasted with algorithms that store and process complete numbers to produce successively more accurate approximations to the desired transcendental.

In mathematics, the digit sum of a natural number in a given number base is the sum of all its digits. For example, the digit sum of the decimal number would be

The Verhoeff algorithm is a checksum for error detection first published by Dutch mathematician Jacobus Verhoeff in 1969. It was the first decimal check digit algorithm which detects all single-digit errors, and all transposition errors involving two adjacent digits, which was at the time thought impossible with such a code.

The Luhn mod N algorithm is an extension to the Luhn algorithm that allows it to work with sequences of values in any even-numbered base. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or any arbitrary set of N characters where N is divisible by 2.

The digital root of a natural number in a given radix is the value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. For example, in base 10, the digital root of the number 12345 is 6 because the sum of the digits in the number is 1 + 2 + 3 + 4 + 5 = 15, then the addition process is repeated again for the resulting number 15, so that the sum of 1 + 5 equals 6, which is the digital root of that number. In base 10, this is equivalent to taking the remainder upon division by 9, which allows it to be used as a divisibility rule.

<span class="mw-page-title-main">IMO number</span> International ship identification number

The IMO number of the International Maritime Organization is a generic term covering two distinct meanings. The IMO ship identification number is a unique ship identifier; the IMO company and registered owner identification number is used to identify uniquely each company and/or registered owner managing ships of at least 100 gross tons (gt). The schemes are managed in parallel, but IMO company/owner numbers may also be obtained by managers of vessels not having IMO ship numbers. IMO numbers were introduced to improve maritime safety and reduce fraud and pollution, under the International Convention for the Safety of Life at Sea (SOLAS).

A payment card number, primary account number (PAN), or simply a card number, is the card identifier found on payment cards, such as credit cards and debit cards, as well as stored-value cards, gift cards and other similar cards. In some situations the card number is referred to as a bank card number. The card number is primarily a card identifier and may not directly identify the bank account number(s) to which the card is/are linked by the issuing entity. The card number prefix identifies the issuer of the card, and the digits that follow are used by the issuing entity to identify the cardholder as a customer and which is then associated by the issuing entity with the customer's designated bank accounts. In the case of stored-value type cards, the association with a particular customer is only made if the prepaid card is reloadable. Card numbers are allocated in accordance with ISO/IEC 7812. The card number is typically embossed on the front of a payment card, and is encoded on the magnetic stripe and chip, but may also be imprinted on the back of the card.

In computational number theory, Cipolla's algorithm is a technique for solving a congruence of the form

<span class="mw-page-title-main">Resident Identity Card</span> Identity document of China

The Resident Identity Card is an official identity document for personal identification in the People's Republic of China. According to the second chapter, tenth clause of the Resident Identity Card Law, residents are required to apply for resident identity cards from the local Public Security Bureau, sub-bureaus or local executive police stations.

References

  1. 1 2 USpatent 2950048A, Luhn, Hans Peter,"Computer for Verifying Numbers",published 23 August 1960,issued 23 August 1960
  2. "Annex B: Luhn formula for computing modulus-10 "double-add-double" check digits". Identification cards — Identification of issuers — Part 1: Numbering system (standard). International Organization for Standardization & International Electrotechnical Commission. January 2017. ISO/IEC 7812-1:2017.
  3. Publication 199: Intelligent Mail Package Barcode (IMpb) Implementation Guide for Confirmation Services and Electronic Payment Systems (PDF) (28th ed.). United States: United States Postal Service. 10 October 2023. Archived (PDF) from the original on 17 November 2023. Retrieved 29 November 2023.
  4. Albanese, Ilenia (10 August 2022). "A cosa serve la Partita Iva? Ecco cosa sapere" [What is a VAT number for? Here's what to know]. Partitaiva.it (in Italian). Archived from the original on 29 June 2024. Retrieved 29 June 2024.