Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
299 views

18th July 2012 Generating and Validating Number Using Luhn Algorithm

The document discusses the Luhn algorithm, which is used to generate and validate identification numbers. It describes how the algorithm works by doubling every other digit starting from the last one and summing the results to calculate a checksum digit. An example is provided of generating a 20 digit number using the Luhn formula and validating it. The author implements pseudo code in C# to demonstrate generating a checksum digit and validating a number. In conclusion, the Luhn algorithm is widely adopted by organizations for tasks like credit card number validation.

Uploaded by

rob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
299 views

18th July 2012 Generating and Validating Number Using Luhn Algorithm

The document discusses the Luhn algorithm, which is used to generate and validate identification numbers. It describes how the algorithm works by doubling every other digit starting from the last one and summing the results to calculate a checksum digit. An example is provided of generating a 20 digit number using the Luhn formula and validating it. The author implements pseudo code in C# to demonstrate generating a checksum digit and validating a number. In conclusion, the Luhn algorithm is widely adopted by organizations for tasks like credit card number validation.

Uploaded by

rob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

18th July 2012

Generating and Validating number using Luhn Algorithm

Generating and Validating any number using Luhn Algorithm,


Luhn Algorithm is also called as a modulus 10, mod 10
You can find more details about this algorithm on Wikipedia page [http://en.wikipedia.org/wiki/Luhn_algorithm] and also
you will get tons of post on how to do it in c# also.
I am just trying here to re-invent the wheel as per my understandings on this topic.
What is this - Wiki says 'Is a simple check-sum formula used to validate a variety of identification numbers'
What it does - This algorithm is used for generating numbers with certain length, it could be for credit card numbers or
IMEI numbers or for any other number.
Why it required - many times user inputed entries can be done with typo mistakes, or scrambling of numbers like 1 to 7, 23
to 32 etc so how to validate whether entered number is correct or not? And the easiest solution is to use Luhn formula,
though it has some drawback but still it serves the basic purpose.
So using Luhn formula 'Extra' digit added to the last in number. 1234 becomes 12345 here 5 is called as check-sum
digit, which helps to validate complete number. So even if someone scrambles or enters incorrect data, using this algorithm
it will be validated.
So in this process the number should be generated with check-sum digit using Luhn formula later on with the same
formula it can be validated.
Who uses this - Most of the Credit card numbers are generated using this formula, also same with IMEI numbers also.
How Pseudo Implementation of this formula in c# .net as follows.
1. In order to use Luhn formula we need to generate the number using Luhn formula.
2. Validating the number with the same formula.
1. Example - Generating 20 digit number using Luhn Formula,
Step 1 - Pass any 19 digit number so that it will generate check-sum number and append it to last, we will pass
"1234567891234567891"
Step 2 - Generate check digit, based on passed numbers,

//this function will return check digit


//it will double the every alternate digit starting from last one,
//in our case it will take "1" and than other alternate digit.
//sum of all the doubled digit and excluded digits
//do modulus 10 on sum and the reminder-10 will give u the check digit and that's it
private static string GetLuhnCheckDigit(string number)
{
var sum = 0;
var alt = true;
var digits = number.ToCharArray();
for (int i = digits.Length - 1; i >= 0; i--)

{
var curDigit = (digits[i] - 48);
if (alt)
{
curDigit *= 2;
if (curDigit > 9)
curDigit -= 9;
}
sum += curDigit;
alt = !alt;
}
if ((sum % 10) == 0 )
{
return "0";
}
return (10 - (sum % 10)).ToString();
}

Step 3 - Add check digit obtained from step 2, (output of above function is 8)
So append the output into our original number so the final number including checksum digit is 12345678912345678918.
2. Example - Generating 20 digit number using Luhn Formula, to validate number using Luhn formula, it has to divisible by
10 directly giving reminder 0.
Step1 - pass the number to validate using Luhn formula, we will pass the outcome of our above example that is
12345678912345678918
Step 2 - Validate the number

// method will double every alternate digit starting from the send digit from last
// sum of doubled digits and all other digits,
// if produces 0 if we do modulus onto them than our number is valid else not.
public static bool PassesTheLuhnCheck(string Number)
{
var total = 0;
var alt = false;
var digits = Number.ToCharArray();
for (int i = digits.Length - 1; i >= 0; i--)
{
var curDigit = (int)char.GetNumericValue(digits[i]);
if (alt)
{
curDigit *= 2;
if (curDigit > 9)
curDigit -= 9;
}
total += curDigit;
alt = !alt;
}
return total % 10 == 0;
}

Conclusion,
Luhn formula is widely used and accepted by major organizations.
further readings,
- http://en.wikipedia.org/wiki/Luhn_algorithm [http://en.wikipedia.org/wiki/Luhn_algorithm%20]
- http://www.codeproject.com/Articles/2782/Credit-Card-Validator-control-for-ASP-NET
[http://www.codeproject.com/Articles/2782/Credit-Card-Validator-control-for-ASP-NET]
- http://www.c-sharpcorner.com/Forums/Thread/102218/ [http://www.c-sharpcorner.com/Forums/Thread/102218/]
- http://en.wikipedia.org/wiki/Credit_card_number [http://en.wikipedia.org/wiki/Credit_card_number]

Happy coding!!

Posted 18th July 2012 by Parag Yerawar


Labels: 20 digits, c#. .net, card number, credit card number, imei number, Luhn, Luhn Algorithm, number
generation, number validation
1

khizer jalal May 20, 2014 at 8:50 AM


Goodiii.. Thanks :)
Reply

Enter your comment...

Comment as:

Publish

Google Account

Preview

View comments

You might also like