IBANCheckDigit.java

  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. import java.io.Serializable;

  19. /**
  20.  * <b>IBAN</b> (International Bank Account Number) Check Digit calculation/validation.
  21.  * <p>
  22.  * This routine is based on the ISO 7064 Mod 97,10 check digit calculation routine.
  23.  * <p>
  24.  * The two check digit characters in a IBAN number are the third and fourth characters
  25.  * in the code. For <i>check digit</i> calculation/validation the first four characters are moved
  26.  * to the end of the code.
  27.  *  So <code>CCDDnnnnnnn</code> becomes <code>nnnnnnnCCDD</code> (where
  28.  *  <code>CC</code> is the country code and <code>DD</code> is the check digit). For
  29.  *  check digit calculation the check digit value should be set to zero (i.e.
  30.  *  <code>CC00nnnnnnn</code> in this example.
  31.  * <p>
  32.  * Note: the class does not check the format of the IBAN number, only the check digits.
  33.  * <p>
  34.  * For further information see
  35.  *  <a href="https://en.wikipedia.org/wiki/International_Bank_Account_Number">Wikipedia -
  36.  *  IBAN number</a>.
  37.  *
  38.  * @since 1.4
  39.  */
  40. public final class IBANCheckDigit extends AbstractCheckDigit implements Serializable {

  41.     private static final int MIN_CODE_LEN = 5;

  42.     private static final long serialVersionUID = -3600191725934382801L;

  43.     private static final int MAX_ALPHANUMERIC_VALUE = 35; // Character.getNumericValue('Z')

  44.     /** Singleton IBAN Number Check Digit instance */
  45.     public static final CheckDigit IBAN_CHECK_DIGIT = new IBANCheckDigit();

  46.     private static final long MAX = 999999999;

  47.     private static final long MODULUS = 97;

  48.     /**
  49.      * Constructs Check Digit routine for IBAN Numbers.
  50.      */
  51.     public IBANCheckDigit() {
  52.     }

  53.     /**
  54.      * Calculate the <i>Check Digit</i> for an IBAN code.
  55.      * <p>
  56.      * <b>Note:</b> The check digit is the third and fourth
  57.      * characters and is set to the value "<code>00</code>".
  58.      *
  59.      * @param code The code to calculate the Check Digit for
  60.      * @return The calculated Check Digit as 2 numeric decimal characters, e.g. "42"
  61.      * @throws CheckDigitException if an error occurs calculating
  62.      * the check digit for the specified code
  63.      */
  64.     @Override
  65.     public String calculate(String code) throws CheckDigitException {
  66.         if (code == null || code.length() < MIN_CODE_LEN) {
  67.             throw new CheckDigitException("Invalid Code length=" + (code == null ? 0 : code.length()));
  68.         }
  69.         code = code.substring(0, 2) + "00" + code.substring(4); // CHECKSTYLE IGNORE MagicNumber
  70.         final int modulusResult = calculateModulus(code);
  71.         final int charValue = 98 - modulusResult; // CHECKSTYLE IGNORE MagicNumber
  72.         final String checkDigit = Integer.toString(charValue);
  73.         return charValue > 9 ? checkDigit : "0" + checkDigit; // CHECKSTYLE IGNORE MagicNumber
  74.     }

  75.     /**
  76.      * Calculate the modulus for a code.
  77.      *
  78.      * @param code The code to calculate the modulus for.
  79.      * @return The modulus value
  80.      * @throws CheckDigitException if an error occurs calculating the modulus
  81.      * for the specified code
  82.      */
  83.     private int calculateModulus(final String code) throws CheckDigitException {
  84.         final String reformattedCode = code.substring(4) + code.substring(0, 4); // CHECKSTYLE IGNORE MagicNumber
  85.         long total = 0;
  86.         for (int i = 0; i < reformattedCode.length(); i++) {
  87.             final int charValue = Character.getNumericValue(reformattedCode.charAt(i));
  88.             if (charValue < 0 || charValue > MAX_ALPHANUMERIC_VALUE) {
  89.                 throw new CheckDigitException("Invalid Character[" + i + "] = '" + charValue + "'");
  90.             }
  91.             total = (charValue > 9 ? total * 100 : total * 10) + charValue; // CHECKSTYLE IGNORE MagicNumber
  92.             if (total > MAX) {
  93.                 total %= MODULUS;
  94.             }
  95.         }
  96.         return (int) (total % MODULUS);
  97.     }

  98.     /**
  99.      * Validate the check digit of an IBAN code.
  100.      *
  101.      * @param code The code to validate
  102.      * @return {@code true} if the check digit is valid, otherwise
  103.      * {@code false}
  104.      */
  105.     @Override
  106.     public boolean isValid(final String code) {
  107.         if (code == null || code.length() < MIN_CODE_LEN) {
  108.             return false;
  109.         }
  110.         final String check = code.substring(2, 4); // CHECKSTYLE IGNORE MagicNumber
  111.         if ("00".equals(check) || "01".equals(check) || "99".equals(check)) {
  112.             return false;
  113.         }
  114.         try {
  115.             return calculateModulus(code) == 1;
  116.         } catch (final CheckDigitException ex) {
  117.             return false;
  118.         }
  119.     }

  120. }