Sunday, 29 March 2020

You have entered a wrong credit card number : dcode.luhn



   

You have entered a wrong credit card number : dcode.luhn                    




 We  are  surrounded by numbers from everywhere , like our pin , password etc;
These numbers really amazed me when I was buying something from Flipkart &accidentally entered a digit wrong of my card number in payment page .It displayed the message as shown below:








After surfing the internet I  learnt about an interesting algorithm behind our card numbers called Luhn's Algorithm.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., so we're gonna dcode.luhn.

Logic behind numbers

    
Step 1:
Multiply the digits at odd position by 2.






Step 2: 
If the digits multiplied by 2 becomes two digit no or no. greater than 10 then add them . For eg: 
5×2= 10 >>>1+0 = 1. 
7×2= 14 >>>1+4 = 5. 
9×2= 18 >>> 1+8 = 7.



Step 3:
Now add the new odd places digits & even places digits.
1+4+2+2+5+5+6+4+1+6+5+8+9+0+2+0 = 60


Step 4:
Check S the sum obtained in step 3 is divisible by 10 or not ,if it is then the card is valid otherwise not .


so 60/10 Remainder = 0 [true].


Therefore, the above example we use is a valid card number.

Click this link for validating

a different card numbers inputs


Note: dcode.luhn was designed to protect against accidental errors, not malicious attacks



Pseudocode [dcode.luhn]



function checkLuhn(string purportedCC) {
    int sum := integer(purportedCC[length(purportedCC)-1])
    int nDigits := length(purportedCC)
    int parity := nDigits modulus 2
    for i from 0 to nDigits - 2 {
        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
}

Applications


  • IMEI numbers
  • National Provider Identification number
  • Survey codes appearing on MC Donalds' ,Taco Bell etc.
  • Social Security Numbers .....so on



Strength

•It can detect any single error.


Weakness:

• Even a 16 digit 0 code will be  a valid card number.
Transposition of two digits 09 to 90 [vice-versa] is not detectable.
• It can't detect in twin digits 22 <-->55 
  For eg:
      22......... till 16 digits

           2+(4)=6.       [ 2×2=4]     
     
     55..... till 16 digits
        5×(1+0)= 6      [5×2=10]

One more thing I didn't mentioned other things  in are also considered validation

"Comment your queries ,love below &; don't forget to share it"



No comments:

Post a Comment

Subarray with given sum

Given: Unsorted array  Find: Subarray starting index  to last index on summing up gives the given sum arr[] = {1, 4, 20, 3, 10, 5}, sum = 33...