12,550,583 members (44,155 online)
alternative version

40.8K views
12 bookmarked
Posted

# Luhn' s Algorithm

, 8 Dec 2004
 Rate this:
Calculating checksum number using Luhn's algorithm

## Introduction

This sample implements the functionality of generating checksum number using Luhn's algorithm.

I've created a class LuhnAlgorithm with a static function:

public static int GetLuhnAlgorithmNumber(string data);

Return value is the generated number.

## Definition

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, was developed in the 1960s as a method of validating identification numbers. It is a simple checksum formula used to validate a variety of account numbers, such as credit card numbers and Canadian Social Insurance Numbers. Much of its notoriety comes from credit card companies' adoption of it shortly after its creation in the late 1960s by IBM scientist Hans Peter Luhn (1896–1964).

Explanation of the Luhn's algorithm can be found here.

A list of licenses authors might use can be found here

## Share

 Austria
No Biography provided

## You may also be interested in...

 Pro Pro

 First Prev Next
 Actually, this method only needs one line changed to compute the correct Luhn check-digit (the "return" equation is wrong) kirkaiya6-Aug-09 18:42 kirkaiya 6-Aug-09 18:42
 This is an old article, so maybe nobody will read this, but - To the people who posted that the method should start with the second-to-last digit - I don't think that is correct. The author seems (from the title) to have intended this as a function to generate the check-digit, not to validate. That said, his "return" equation has an error. It should be (expressed in VB.Net, which I used): `Return CInt((Math.Truncate(sum / 10) + 1) * 10) - sum` [not (((sum / 10) + 1) * 10) - sum, which is what the source-code has] In other words - the author's code only fails to truncate the result of sum/10, which leads to equation returning 10 [since, for example, if sum = 6, then (((sum/10)+1)*10 - sum = 10 ]. After changing the line indicated above, and typing in all but the last digit of four different credit-card numbers (eg., give it a string of 15 digits, don't include the last "check-digit"), the function correctly returned the final digit of all four card-numbers. Here's the entire (corrected)_function in VB.Net for anybody who is too lazy to convert: ```Public Shared Function GetChecksum(ByVal DecimalString As String) As Integer Dim sum As Integer = 0 Dim odd As Boolean = True For i As Integer = DecimalString.Length - 1 To 0 Step -1 If odd = True Then Dim tSum As Integer = Convert.ToInt32(DecimalString(i).ToString()) * 2 If tSum >= 10 Then Dim tData As String = tSum.ToString() tSum = Convert.ToInt32(tData(0).ToString()) + Convert.ToInt32(tData(1).ToString()) End If sum += tSum Else sum += Convert.ToInt32(DecimalString(i).ToString()) End If odd = Not odd Next   Return CInt((Math.Truncate(sum / 10) + 1) * 10) - sum   End Function```
 Incorrect Algorithm? Peebles3-Mar-05 2:36 Peebles 3-Mar-05 2:36
 use this code... Joel Thoms18-Jan-05 11:47 Joel Thoms 18-Jan-05 11:47
 how to use Joel Thoms18-Jan-05 11:41 Joel Thoms 18-Jan-05 11:41
 wrote an implementation myslef some time ago MadHatter ¢9-Dec-04 7:02 MadHatter ¢ 9-Dec-04 7:02
 Explaining my vote Jason De Arte8-Dec-04 8:45 Jason De Arte 8-Dec-04 8:45
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Oct-16 11:47 Refresh 1