arturmkrtchyan / iban4j

A Java library for generation and validation of the International Bank Account Numbers (IBAN ISO_13616) and Business Identifier Codes (BIC ISO_9362).
http://iban4j.org
Apache License 2.0
274 stars 124 forks source link

Working random IBAN generation for France #46

Open nvuillam opened 6 years ago

nvuillam commented 6 years ago

For French IBANs, there can be different consistency checks

So random generated IBANs with Country.FR are not valid.

Here is a workaround for anyone looking for it ( maybe you can integrate it in your library if you like )

      String bankCode = "30006" ;
      String branchCode = "00001" ;
      Random r = new Random();
      int Low = 10;
      int High = 1000000;
      int AccountNumberInt = r.nextInt(High-Low) + Low;
      String AccountNumberStr = String.valueOf(AccountNumberInt);
      String AccountNumber = StringUtils.repeat("0", 11 - AccountNumberStr.length()) + AccountNumberStr;
      long a100000000000 =  (long) 100000000000.0;
      Long nationalCheckDigitInt = 97 - (((Long.valueOf(bankCode) % 97 * 100000 + Integer.valueOf(branchCode)) % 97 * a100000000000 + Integer.valueOf(AccountNumber)) % 97) * 100 % 97;
       String nationalCheckDigit = String.valueOf(nationalCheckDigitInt) ;
       Iban iban = new Iban.Builder().countryCode(CountryCode.FR).bankCode(bankCode).branchCode(branchCode).accountNumber(AccountNumber).nationalCheckDigit(nationalCheckDigit).buildRandom();  
thecoder8890 commented 2 years ago

This is more effective

String bankCode = "30006" ; String branchCode = "00001" ; Random r = new Random(); int Low = 10; int High = 1000000; int AccountNumberInt = r.nextInt(High-Low) + Low; String AccountNumberStr = String.valueOf(AccountNumberInt); String AccountNumber = StringUtils.repeat("0", 11 - AccountNumberStr.length()) + AccountNumberStr; long a100000000000 = (long) 100000000000.0; Long nationalCheckDigitInt = 97 - (((Long.valueOf(bankCode) % 97 * 100000 + Integer.valueOf(branchCode)) % 97 * a100000000000 + Long.valueOf(AccountNumber)) % 97) * 100 % 97; String nationalCheckDigit = String.valueOf(nationalCheckDigitInt) ; Iban iban = new Iban.Builder().countryCode(CountryCode.FR).bankCode(bankCode).branchCode(branchCode).accountNumber(AccountNumber).nationalCheckDigit(nationalCheckDigit).buildRandom();

bllakinci commented 11 months ago

The basic formula to calculate nationalCheckDigit for FR is like this;

  @param bankCode 5 character
  @param branchCode 5 character
  @param accountNumber 11 character

  cleRibKey = - (bankCode * pow(10,18)  + branchCode * pow(10,13) + accountNumber * pow(10,2))  % 97
  cleRibKey = - (bankCode * 89          + branchCode * 15         + accountNumber * 3)          % 97

If you want to create an account number that contains letters you need to replace characters in the account number with the below numbers. After that, you can calculate the cleRibKey with the above formula.

A, J = 1
B, K, S = 2
C, L, T = 3
D, M, U = 4
E, N, V = 5
F, O, W = 6
G, P, X = 7
H, Q, Y = 8
I, R, Z = 9