View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines.checkdigit;
18  
19  import java.io.Serializable;
20  
21  /**
22   * <b>IBAN</b> (International Bank Account Number) Check Digit calculation/validation.
23   * <p>
24   * This routine is based on the ISO 7064 Mod 97,10 check digit calculation routine.
25   * <p>
26   * The two check digit characters in a IBAN number are the third and fourth characters
27   * in the code. For <i>check digit</i> calculation/validation the first four characters are moved
28   * to the end of the code.
29   *  So <code>CCDDnnnnnnn</code> becomes <code>nnnnnnnCCDD</code> (where
30   *  <code>CC</code> is the country code and <code>DD</code> is the check digit). For
31   *  check digit calculation the check digit value should be set to zero (i.e.
32   *  <code>CC00nnnnnnn</code> in this example.
33   * <p>
34   * Note: the class does not check the format of the IBAN number, only the check digits.
35   * <p>
36   * For further information see
37   *  <a href="http://en.wikipedia.org/wiki/International_Bank_Account_Number">Wikipedia -
38   *  IBAN number</a>.
39   *
40   * @version $Revision: 1713572 $
41   * @since Validator 1.4
42   */
43  public final class IBANCheckDigit implements CheckDigit, Serializable {
44  
45      private static final long serialVersionUID = -3600191725934382801L;
46  
47      /** Singleton IBAN Number Check Digit instance */
48      public static final CheckDigit IBAN_CHECK_DIGIT = new IBANCheckDigit();
49  
50      private static final long MAX = 999999999;
51  
52      private static final long MODULUS = 97;
53  
54      /**
55       * Construct Check Digit routine for IBAN Numbers.
56       */
57      public IBANCheckDigit() {
58      }
59  
60      /**
61       * Validate the check digit of an IBAN code.
62       *
63       * @param code The code to validate
64       * @return <code>true</code> if the check digit is valid, otherwise
65       * <code>false</code>
66       */
67      public boolean isValid(String code) {
68          if (code == null || code.length() < 5) {
69              return false;
70          }
71          String check = code.substring(2,4);
72          if ("00".equals(check) || "01".equals(check) || "99".equals(check)) {
73              return false;
74          }
75          try {
76              int modulusResult = calculateModulus(code);
77              return (modulusResult == 1);
78          } catch (CheckDigitException  ex) {
79              return false;
80          }
81      }
82  
83      /**
84       * Calculate the <i>Check Digit</i> for an IBAN code.
85       * <p>
86       * <b>Note:</b> The check digit is the third and fourth
87       * characters and is set to the value "<code>00</code>".
88       *
89       * @param code The code to calculate the Check Digit for
90       * @return The calculated Check Digit as 2 numeric decimal characters, e.g. "42"
91       * @throws CheckDigitException if an error occurs calculating
92       * the check digit for the specified code
93       */
94      public String calculate(String code) throws CheckDigitException {
95          if (code == null || code.length() < 5) {
96              throw new CheckDigitException("Invalid Code length=" +
97                      (code == null ? 0 : code.length()));
98          }
99          code = code.substring(0, 2) + "00" + code.substring(4);
100         int modulusResult = calculateModulus(code);
101         int charValue = (98 - modulusResult);
102         String checkDigit = Integer.toString(charValue);
103         return (charValue > 9 ? checkDigit : "0" + checkDigit);
104     }
105 
106     /**
107      * Calculate the modulus for a code.
108      *
109      * @param code The code to calculate the modulus for.
110      * @return The modulus value
111      * @throws CheckDigitException if an error occurs calculating the modulus
112      * for the specified code
113      */
114     private int calculateModulus(String code) throws CheckDigitException {
115         String reformattedCode = code.substring(4) + code.substring(0, 4);
116         long total = 0;
117         for (int i = 0; i < reformattedCode.length(); i++) {
118             int charValue = Character.getNumericValue(reformattedCode.charAt(i));
119             if (charValue < 0 || charValue > 35) {
120                 throw new CheckDigitException("Invalid Character[" +
121                         i + "] = '" + charValue + "'");
122             }
123             total = (charValue > 9 ? total * 100 : total * 10) + charValue;
124             if (total > MAX) {
125                 total = total % MODULUS;
126             }
127         }
128         return (int)(total % MODULUS);
129     }
130 
131 }