`

Credit Card Mod10 校验

 
阅读更多

以下是几种Mod10的实现。第一种最为简洁,最后一种最为易懂。你喜欢哪一种?

    /**

     * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

     * <br/>

     * (Getting from http://en.wikipedia.org/wiki/Luhn_algorithm)<br/>

     * 1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.<br/>

     * 2. Sum the digits of the products together with the undoubled digits from the original number.<br/>

     * 3. If the total ends in 0 (put another way, if the total modulo 10 is equal to 0), then the number

     *    is valid according to the Luhn formula; else it is not valid.<br/>

     * <br/>

     * As an illustration, if the account number is 49927398716, it will be validated as follows:<br/>

     * i.   Double every second digit, from the rightmost:<br/>

     *     (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18<br/>

     * ii.  Sum all the individual digits (digits in parentheses are the products from Step 1):<br/>

     *      6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70<br/>

     * iii. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.<br/>

     *

     * @param num

     * @return boolean true-valid; false-invalid

     */

    public static boolean isValidCC(String num) {

//          //Check digits

//          if(!num.matches("^\\d{13,19}$"))

//                return false;

           

            //Check mod10

            final int[][] sumTable = {{0,1,2,3,4,5,6,7,8,9},{0,2,4,6,8,1,3,5,7,9}};

            int sum = 0, flip = 0;

           

            for (int i = num.length() - 1; i >= 0; i--, flip++){

                  int n = num.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

                  sum += sumTable[flip & 0x1][n];

            }

            return (sum == 0) ? false : (sum % 10 == 0);

        }

     

    /**

     * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

     * Used switch to double specified value.<br/>

     *

     * @param number

     * @return

     */

      public static boolean isValidCC1(String number) {

            int sum = 0;

           

            int mul = 1;

            for (int i = number.length() - 1; i >= 0; i--) {

                  int n = number.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

                 

                  n *= (mul == 1) ? mul++ : mul--;

                  sum += (n>9 ? n-9 : n);

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

     

      /**

       * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

       * Used position to double specified value.<br/>

       *

       * @param number

       * @return

       */

      public static boolean isValidCC2(String number) {

            int sum = 0;

           

            for (int i = number.length() - 1; i >= 0; i--) {

                  int n = number.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

 

                  if ((number.length()-i)%2 == 0)

                        n *= 2;

 

                  sum += (n>9 ? n-9 : n);

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

     

      /**

       * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

       * Used AND and XOR to double specified value.<br/>

       * @param number

       * @return

       */

      public static boolean isValidCC3(String number) {

            int digits = number.length();

            int oddOrEven = digits & 1;

            long sum = 0;

            for (int i = 0; i < digits; i++) {

                  int n = number.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

                 

                  if (((i & 1) ^ oddOrEven) == 0)

                        n *= 2;

                  sum += (n>9 ? n-9 : n);

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

 

      /**

       * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

       * Used a variable as switch to double specified value.

       * @param number

       * @return

       */

      private static boolean isValidCC4(String number) {

            int sum = 0;

 

            boolean alternate = false;

            for (int i = number.length() - 1; i >= 0; i--) {

                  int n = Integer.parseInt(number.substring(i, i + 1)); //May throw out a runtime exception

                  if (n < 0 || n > 9)

                        return false;

                  if (alternate)

                        n *= 2;

                  sum += (n>9 ? n%10 + 1 : n);

                  alternate = !alternate;

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

 

信用卡前缀及校验规则

CARD TYPE Prefix Length Check digit algorithm
MASTERCARD 51-55 16 mod 10
VISA 4 13, 16 mod 10
AMEX 34 
37
15 mod 10
Diners Club/
Carte Blanche
300-305
36
38
14 mod 10
Discover 6011 16 mod 10
enRoute 2014
2149
15 any
JCB 3 16 mod 10
JCB 2131
1800
15 mod 10

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics