| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | import org.jcoderz.commons.ArgumentMalformedException; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * The Luhn algorithm. |
|---|
| 39 | * |
|---|
| 40 | * The Luhn algorithm or Luhn formula, also known as the |
|---|
| 41 | * <b>modulus 10</b> or <b>mod 10</b> algorithm, was developed |
|---|
| 42 | * in the 1960s as a method of validating identification numbers. |
|---|
| 43 | * It is a simple checksum formula used to validate a variety |
|---|
| 44 | * of account numbers, such as credit card numbers and Canadian |
|---|
| 45 | * Social Insurance Numbers. |
|---|
| 46 | * Much of its notoriety comes from credit card companies' adoption |
|---|
| 47 | * of it shortly after its creation in the late 1960s by |
|---|
| 48 | * IBM scientist Hans Peter Luhn (1896-1964). |
|---|
| 49 | * |
|---|
| 50 | * The algorithm is in the public domain and is in wide use today. |
|---|
| 51 | * It is not intended to be a cryptographically secure hash function; |
|---|
| 52 | * it protects against random error, not malicious attack. |
|---|
| 53 | * Most credit cards and many government identification numbers |
|---|
| 54 | * use the algorithm as a simple method of distinguishing valid |
|---|
| 55 | * numbers from collections of random digits. |
|---|
| 56 | * |
|---|
| 57 | * @author Michael Griffel |
|---|
| 58 | */ |
|---|
| 59 | public final class LuhnAlgorithm |
|---|
| 60 | { |
|---|
| 61 | private static final int MAX_DIGIT = 9; |
|---|
| 62 | private static final int MODULUS = 10; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Private Constructor. |
|---|
| 66 | */ |
|---|
| 67 | private LuhnAlgorithm () |
|---|
| 68 | { |
|---|
| 69 | // provides only static methods - no instances allowed. |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Execute the Luhn (Mod10) card check on a given card number. |
|---|
| 74 | * |
|---|
| 75 | * @param cardNumber the card number to check. |
|---|
| 76 | * @return <tt>true</tt> if the Luhn check succeeds; |
|---|
| 77 | * <tt>false</tt> otherwise. |
|---|
| 78 | */ |
|---|
| 79 | public static boolean check (String cardNumber) |
|---|
| 80 | { |
|---|
| 81 | final String s = reverseString(cardNumber); |
|---|
| 82 | int crossSum = 0; |
|---|
| 83 | for (int i = 0; i < s.length(); i++) |
|---|
| 84 | { |
|---|
| 85 | final int digit = s.charAt(i) - '0'; |
|---|
| 86 | |
|---|
| 87 | if (isOdd(i)) |
|---|
| 88 | { |
|---|
| 89 | crossSum += computeCrossSum(digit); |
|---|
| 90 | } |
|---|
| 91 | else |
|---|
| 92 | { |
|---|
| 93 | crossSum += digit; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | return (crossSum % MODULUS == 0); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Computes the Luhn check digit for a given card number. |
|---|
| 102 | * |
|---|
| 103 | * @param cardNumberWithoutLastDigit the card number without |
|---|
| 104 | * the last digit. |
|---|
| 105 | * @return the Luhn check digit for the given card number. |
|---|
| 106 | */ |
|---|
| 107 | public static int computeLuhnCardNumber (String cardNumberWithoutLastDigit) |
|---|
| 108 | { |
|---|
| 109 | final String s = reverseString(cardNumberWithoutLastDigit); |
|---|
| 110 | |
|---|
| 111 | int crossSum = 0; |
|---|
| 112 | for (int i = 0; i < s.length(); i++) |
|---|
| 113 | { |
|---|
| 114 | final int digit = s.charAt(i) - '0'; |
|---|
| 115 | if (isEven(i)) |
|---|
| 116 | { |
|---|
| 117 | crossSum += computeCrossSum(digit); |
|---|
| 118 | } |
|---|
| 119 | else |
|---|
| 120 | { |
|---|
| 121 | crossSum += digit; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | crossSum %= MODULUS; |
|---|
| 125 | |
|---|
| 126 | return (MODULUS - crossSum) % MODULUS; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | private static boolean isEven (int i) |
|---|
| 131 | { |
|---|
| 132 | return (i & 1) == 0; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | private static boolean isOdd (int i) |
|---|
| 136 | { |
|---|
| 137 | return (i & 1) == 1; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | private static int computeCrossSum (int number) |
|---|
| 141 | { |
|---|
| 142 | if (number > MAX_DIGIT) |
|---|
| 143 | { |
|---|
| 144 | throw new ArgumentMalformedException("number", |
|---|
| 145 | String.valueOf(number), "Number must be between 0 and 9"); |
|---|
| 146 | } |
|---|
| 147 | final int i = number << 1; |
|---|
| 148 | return (i > MAX_DIGIT ? i - MAX_DIGIT : i); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private static String reverseString (String s) |
|---|
| 152 | { |
|---|
| 153 | final char[] source = s.toCharArray(); |
|---|
| 154 | final char[] reversed = new char[source.length]; |
|---|
| 155 | for (int i = 0; i < reversed.length; i++) |
|---|
| 156 | { |
|---|
| 157 | reversed[i] = source[reversed.length - i - 1]; |
|---|
| 158 | } |
|---|
| 159 | return String.valueOf(reversed); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|