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 U.S. Patent No. 2,950,048, granted on August 23, 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. If the number already contains the check digit, drop that digit to form the "payload". The check digit is most often the last digit.
  2. With the payload, start from the rightmost digit. Moving left, double the value of every second digit (including the rightmost digit).
  3. Sum the values of the resulting digits.
  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)) end function

Code implementation

C#

boolIsValidLuhn(inint[]digits){intcheck_digit=0;for(inti=digits.Length-2;i>=0;--i)check_digit+=((i&1)is0)switch{true=>digits[i]>4?digits[i]*2-9:digits[i]*2,false=>digits[i]};return(10-(check_digit%10))%10==digits.Last();}

Java

publicstaticbooleanisValidLuhn(Stringnumber){intn=number.length();inttotal=0;booleaneven=true;// iterate from right to left, double every 'even' valuefor(inti=n-2;i>=0;i--){intdigit=number.charAt(i)-'0';if(digit<0||digit>9){// value may only contain digitsreturnfalse;}if(even){digit<<=1;// double value}even=!even;total+=digit>9?digit-9:digit;}intchecksum=number.charAt(n-1)-'0';return(total+checksum)%10==0;}

TypeScript

functionluhnCheck(input:number):boolean{constnumber=input.toString();constdigits=number.replace(/\D/g,'').split('').map(Number);letsum=0;letisSecond=false;for(leti=digits.length-1;i>=0;i--){letdigit=digits[i];if(isSecond){digit*=2;if(digit>9){digit-=9;}}sum+=digit;isSecond=!isSecond;}returnsum%10===0;}

Uses

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

Related Research Articles

<span class="mw-page-title-main">Checksum</span> Data used to detect errors in other data

A checksum is a small-sized block of data derived from another block of digital data for the purpose of detecting errors that may have been introduced during its transmission or storage. By themselves, checksums are often used to verify data integrity but are not relied upon to verify data authenticity.

<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.

<span class="mw-page-title-main">International Bank Account Number</span> Alphanumeric code that uniquely identifies a bank account in any participating country

The International Bank Account Number (IBAN) is an internationally agreed upon system of identifying bank accounts across national borders to facilitate the communication and processing of cross border transactions with a reduced risk of transcription errors. An IBAN uniquely identifies the account of a customer at a financial institution. It was originally adopted by the European Committee for Banking Standards (ECBS) and since 1997 as the international standard ISO 13616 under the International Organization for Standardization (ISO). The current version is ISO 13616:2020, which indicates the Society for Worldwide Interbank Financial Telecommunication (SWIFT) as the formal registrar. Initially developed to facilitate payments within the European Union, it has been implemented by most European countries and numerous countries in other parts of the world, mainly in the Middle East and the Caribbean. By July 2023, 86 countries were using the IBAN numbering system.

<span class="mw-page-title-main">Prime number</span> Number divisible only by 1 or itself

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 4 is composite because it is a product (2 × 2) in which both numbers are smaller than 4. Primes are central in number theory because of the fundamental theorem of arithmetic: every natural number greater than 1 is either a prime itself or can be factorized as a product of primes that is unique up to their order.

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.

A CUSIP is a nine-character numeric or alphanumeric code that uniquely identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. All CUSIP identifiers are fungible, which means that a unique CUSIP identifier for each individual security stays the same, regardless of the exchange where the shares were purchased or venue on which the shares were traded. CUSIP was adopted as an American national standard by the Accredited Standards Committee X9 and is designated ANSI X9.6. CUSIP was re-approved as an ANSI standard in December 2020. The acronym derives from Committee on Uniform Security Identification Procedures.

A national identification number, national identity number, or national insurance number or JMBG/EMBG is used by the governments of many countries as a means of tracking their citizens, permanent residents, and temporary residents for the purposes of work, taxation, government benefits, health care, and other governmentally-related functions.

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

<span class="mw-page-title-main">European Community number</span> Identifier for substance regulated within EU

The European Community number is a unique seven-digit identifier that was assigned to substances for regulatory purposes within the European Union by the European Commission. The EC Inventory comprises three individual inventories, EINECS, ELINCS and the NLP list.

In number theory, a narcissistic number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits.

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.

PESEL is the national identification number used in Poland since 1979. The number is 11 digits long, identifies exactly one person, and cannot be changed once assigned, except in specific situations.

A mobile equipment identifier (MEID) is a globally unique number identifying a physical piece of CDMA2000 mobile station equipment. The number format is defined by the 3GPP2 report S.R0048 but in practical terms, it can be seen as an IMEI but with hexadecimal digits.

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.

The cyclic redundancy check (CRC) is based on division in the ring of polynomials over the finite field GF(2), that is, the set of polynomials where each coefficient is either zero or one, and arithmetic operations wrap around.

<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).

<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 P.,"Computer for verifying numbers",published 1960-08-23
  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". United States Postal Service. Retrieved 29 November 2023.