Validate Credit card number using Luhn Algorithm

Validate Credit Card Number with Luhn Algorithm and CVV2

How does the internet company also e-commerce company verify that you have the credit card? It verifies using CVV2 Code and Luhn Algorithm. The Luhn Algorithm is used to validate all sorts of numbers, including credit cards numbers.

Who needs to validate Credit Card Number and Why?

When you buy a thing from a shop and pay by credit card then the shopkeeper sees that you own the card and he swipes it physically. When the card is swiped the data encoded in the chip in front or magnetic stripe on the back is read.

When one buys something on the internet using a credit card then the customer paying using a credit card has to enter the card number, expiration date, and an additional number printed on the card dubbed CVV2.

Validate Credit card number using Luhn Algorithm
Validate Credit card number using Luhn Algorithm

How does the internet company also e-commerce company verify that you have the card? It verifies using CVV2 Code and Luhn Algorithm. The different parts of the card or the Anatomy of the card is shown in the image below.

Anatomy of a credit card Luhn Algorithm
Anatomy of a credit card

CVV

Card security codes are the three-digit numbers found in the signature panels on the back of Visa (Card Verification Value 2 — CVV2), MasterCard (Card Verification Code 2 — CVC 2) etc. These numbers are generated when the card is issued, by hashing the card number and expiration date under a key known only to the card issuer. These CVV codes helps merchants verify that their customers have their cards at the time of the transaction.

Have you thought that if the 3 digit number on back of credit card is called as CVV2 where is CVV1?

There are up to three tracks on magnetic cards known as track 1, 2, and 3. Track 3 is virtually unused by the major worldwide card networks. Point-of-sale card readers almost always read track 1, or track 2, and sometimes both, in case one track is unreadable. Track 1 has a higher bit density (210 bits per inch vs. 75), is the only track that may contain alphabetic text, and hence is the only track that contains the card holder’s name. One of the fields in this track layout is the Card Validation Code (CVC) or CVC1. which serves as a cryptographic integrity check on the track contents. Much like a message authentication code, the CVC simplifies the process of authenticating track data when it is received by the issuing bank.

Tracks on Credit Card
Tracks on Credit Card

Luhn algorithm

The Luhn Algorithm or Luhn formula, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers and Greek Social Security Numbers (ΑΜΚΑ).

Luhn the inventor of Luhn algo
Luhn the inventor of Luhn algo

It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

Let’s see how to validate a credit card number. If you want to see the video on Luhn Algorithm you can click here.

  • Take the credit card number and read the digits from the right. Double every other number and write them down
  • Compute a weighted sum with the weight pattern 2,1,2,1,2,1 etc except that on multiplying a digit by 2 if you get a 2 digit number you add those digits together. For example 7  x 2=14 then it becomes 1+4=5
  • Add up all of the results. If the sum ends in zero, then the credit card number is valid.

Let’s use Luhn algorithm to determine if the credit card number 3379 5135 6110 8795 is valid

Digit 3 3 7 9 5 1 3 5 6 1 1 0 8 7 9 5
Weight 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
Product 6 3 14 9 10 1 6 5 12 1 2 0 16 7 18 5
Modified Product 6 3 5 9 1 1 6 5 3 1 2 0 7 7 9 5

Sum=70. These add up to 70 hence the credit card number is valid.

Use Luhn algorithm to determine check(last) digit of if the credit card number 2769 1483 0405 998?

Digit 2 7 6 9 1 4 8 3 0 4 0 5 9 9 8 ?
Weight 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 ?
Product 4 7 12 9 2 4 16 3 0 4 0 5 18 9 16 ?
Modified Product 4 7 3 9 2 4 7 3 0 4 0 5 9 9 7 ?

These add up to 73 so check digit should be something that when added gives a multiple of 8. Nearest multiple of 10 is 80 so check digit is 80-73=7

Video on Luhn Algorithm

YouTube video which explains the Luhn Algorithm is given below

You can try the website to https://binlist.net/ to find what information do the first 8 digits of your Master/Visa credit/debit card reveal.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.