﻿ Luhn algorithm

# Luhn algorithm

The Luhn algorithm or Luhn formula, also known as the " modulus 10 " - or "mod 10 " algorithm and the Double - Add- Double method, has been in the 1960s by Hans Peter Luhn as a method of verification of identification numbers developed. It is a simple checksum formula is used to confirm a plurality of identification numbers such as credit card numbers, social security numbers, and Canadian. The Luhn algorithm also verified ISINs and the seven -digit account numbers of the German Bank AG and Commerzbank AG, as well as many savings banks.

The algorithm is public domain and very common nowadays. He is not a cryptographic hash function and therefore does not protect prior to random errors but from intentional attacks. Most credit cards and many governmental identification numbers, but also the web at the numbers of locomotives and cars, use the algorithm as a simple method to distinguish valid numbers of random number combinations.

The Luhn algorithm detects any errors in single digits, as well as most permutations of juxtaposed digits. However, it does not detect the reversal of the sequence 09 to 90 or vice versa.

• 5.1 The algorithm in C #

## Informal Explanation

The formula generates a check digit, which is attached usually to the incomplete identification number in order to get the full number. This number must pass the following algorithm:

## Algorithm

The algorithm proceeds in three steps. In the first step, each second number, starting with the second from the right, is doubled. If the result is greater than 9, the cross- sum of the result of the doubling is formed ( 2 to 4, 7 to 5). In the second step, all the numbers are summed. Finally, in the last step, the result is divided by 10. If the remainder is 0, the original sequence of digits that had to verify valid.

## Example implementations

### Pseudo-code

Function checkLuhn (string purportedCC ) {       int sum: = 0       int nDigits: = length ( purportedCC )       int parity: = nDigits modulus 2       for i from 0 to nDigits - 1 {           int digit: = integer ( purportedCC [i])           if i modulus 2 = parity               digit: = digit × 2           if digit > 9               digit: = digit - 9           sum: = sum digit       }       return ( sum modulus 10) = 0   } C

# include / / for atoi   # include / / for strlen     bool checkLuhn (const char * pPurported )   {     NSum int = 0;     int nDigits = strlen ( pPurported );     int nParity = ( nDigits -1) % 2;     cDigit char = " \ 0 \ 0"; / / Atoi expects a null - terminated string     for (int i = nDigits, i> 0, i - )     {       cDigit = pPurported [i -1];       int nDigit = atoi ( cDigit );         if ( i == nParity % 2)         nDigit = nDigit * 2;         NSum = nDigit/10;       NSum = nDigit % 10;     }     return 0 == NSum % 10;   } C #

The code checks whether a string is valid according to the algorithm:

Public static bool CheckLuhn (string data ) {       int sum = 0;       int len = data.Length;       for (int i = 0; i < len; i ) {          int add = ( (int) data [ i] - 48 ) * ( 2 - ( i len ) % 2);          add - add = > 9? 9: 0;          sum = add;       }       return sum % 10 == 0;    } PHP

Function checkLuhn ( \$ number ) {      \$ sum = 0;      \$ numDigits = strlen ( \$ number ) -1;      \$ parity = \$ numDigits % 2;      for (\$ i = \$ numDigits; \$ i > = 0; \$ i - ) {          \$ digit = substr ( \$ number, \$ i, 1);          if (! \$ parity == (\$ i % 2) ) {\$ digit << = 1 ;}          \$ digit = (\$ digit > 9)? (\$ digit - 9 ): \$ digit;          \$ sum = \$ digit;      }      return ( 0 == ( \$ sum % 10 ) ); } Java

Public static boolean check ( int [ ] digits ) {         int sum = 0;         int length = digits.length;         for (int i = 0; i 9? digit - 9: digit;         }         return sum % 10 == 0;     } Python

Def checkLuhn ( purportedCC =''):          sum = 0          parity = len ( purportedCC ) % 2          for i, digit in enumerate ( [int (x ) for x in purportedCC ] ):              if i % 2 == parity:                  digit * = 2              if digit > 9:                  digit - 9 =              sum = digit          return sum % 10 == 0 Haskell

CheckluhnIntegral :: Integral a = > a -> Bool checkluhnIntegral i = ( lunvalIntegral i) == 0   genluhnIntegral :: Integral a = > a -> a genluhnIntegral i = (10 * i) mod ( 10 - lunvalIntegral (10 * i)) 10   lunvalIntegral :: Integral a = > a -> a lunvalIntegral i = mod (single next i) 10                     where                       single next 0 = 0                       single next i = ( i mod 10 ) double next ( i div 10)                       double next 0 = 0                       next double i = ( luhndouble (mod i 10) ) single next ( i div 10)   luhndouble a = DSum ( 2 * a ) DSum a | a <10 = a         | Otherwise = ( DSum ( a div 10 )) ( mod a 10) example

Given the sample identification number 446-667-651.

The sum of 40 is divided by 10; the rest is 0 - ie the number is valid.

## Application for EC cards

With debit cards ( credit / debit ) the calculation of the number is slightly different. Because the check digit in the magnetic stripe must be only one digit is a number that ends in "0", "0" ( 10 = 0,20 = 0, etc ).

In addition, each second number, starting from the right side ( instead of the second from the right) is doubled ( 1010 to 1010, 0101 is about 0202).

### The algorithm in C #

Public static int GetLuhn (string data )         {             int sum = 0;             bool odd = false;             for (int i = data.Length - 1; i> = 0; i - )             {                 if ( odd == false)                 {                     int Tsum = Convert.ToInt32 ( data [ i] ToString (). ) * 2;                     if ( Tsum > = 10)                     {                         Tsum Tsum = - 9;                     }                     sum = Tsum;                 }                 else                     sum = Convert.ToInt32 ( data [ i] ToString (). );                 odd = odd! ;             }             int res = ((( sum / 10) 1) * 10) - sum;             if ( erg.ToString (). EndsWith ("0" ) )             {                 res = 0;             }             return res;         } Web Links

• Online Luhn test
• Theoretical computer science
• Identification technology
292366
de