IBAN, iban4j and CIN calculation

The International Bank Account Number (IBAN) is used to uniquely identify bank details internationally.

The code is as follows:

  • 2 capital letters representing the Nation (IT for Italy)
  • 2 control digits
  • the national BBAN code

For Italy, the BBAN code (Basic Bank Account Number) is composed of:

  • CIN (1 uppercase letter)
  • ABI (5 digits)
  • CAB (5 digits)
  • Account number (12 alphanumeric characters possibly preceded by zeros if the number of characters is less than 12)

The CIN (Control Internal Number) code consists of a single letter and is used as a control character. It’s calculated based on the ABI and CAB codes and account number.

Both the two control digits and the CIN can be calculated to verify that the IBAN entered in a form by a user is valid and compliant. To do this in java there is the iban4j library.

The problem with this library is that it calculates the two control digits but not the CIN. On the net I didn’t find any java library that made the CIN calculation. I only found an example written in Visual Basic  of which I ported in java. The class name is CINUtil and it can be downloaded from PasteBin.

A method for checking the iban inserted in a form by a user can be the following:


public static boolean checkIban(String ibanCode) {
String countryCode = ibanCode.substring(0, 2);
String abi = ibanCode.substring(5, 10);
String cab = iban.substring(10, 15);
String conto = iban.substring(15);


org.iban4j.Iban ibanToCheck = new org.iban4j.Iban.Builder()
.countryCode(CountryCode.valueOf(countryCode))
.bankCode(abi)
.nationalCheckDigit(CINUtil.computeCin(abi, cab, conto))
.branchCode(cab)
.accountNumber(conto)
.build(true);


return ibanCode.equals(ibanToCheck.toString());
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s