Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.LuhnAlgorithm

LineHitsNoteSource
1  /*
2   * $Id: LuhnAlgorithm.java 1011 2008-06-16 17:57:36Z amandel $
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 ()
680    {
69        // provides only static methods - no instances allowed.
700    }
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     {
810       final String s = reverseString(cardNumber);
820       int crossSum = 0;
830       for (int i = 0; i < s.length(); i++)
84        {
850          final int digit = s.charAt(i) - '0';
86  
870          if (isOdd(i))
88           {
890             crossSum += computeCrossSum(digit);
90           }
91           else
92           {
930             crossSum += digit;
94           }
95        }
96  
970       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     {
1090       final String s = reverseString(cardNumberWithoutLastDigit);
110  
1110       int crossSum = 0;
1120       for (int i = 0; i < s.length(); i++)
113        {
1140          final int digit = s.charAt(i) - '0';
1150          if (isEven(i))
116           {
1170             crossSum += computeCrossSum(digit);
118           }
119           else
120           {
1210             crossSum += digit;
122           }
123        }
1240       crossSum %= MODULUS;
125  
1260       return (MODULUS - crossSum) % MODULUS;
127     }
128  
129  
130     private static boolean isEven (int i)
131     {
1320       return (i & 1) == 0;
133     }
134  
135     private static boolean isOdd (int i)
136     {
1370       return (i & 1) == 1;
138     }
139  
140     private static int computeCrossSum (int number)
141     {
1420       if (number > MAX_DIGIT)
143        {
1440(1)         throw new ArgumentMalformedException("number",
145                 String.valueOf(number), "Number must be between 0 and 9");
146        }
1470       final int i = number << 1;
1480       return (i > MAX_DIGIT ? i - MAX_DIGIT : i);
149     }
150  
151     private static String reverseString (String s)
152     {
1530       final char[] source = s.toCharArray();
1540       final char[] reversed = new char[source.length];
1550       for (int i = 0; i < reversed.length; i++)
156        {
1570          reversed[i] = source[reversed.length - i - 1];
158        }
1590       return String.valueOf(reversed);
160     }
161  }

Findings in this File

i (1) 144 : 0 method org.jcoderz.commons.util.LuhnAlgorithm.computeCrossSum(int) throws exception with static message string