iban4j icon indicating copy to clipboard operation
iban4j copied to clipboard

Working random IBAN generation for France

Open nvuillam opened this issue 6 years ago • 2 comments

For French IBANs, there can be different consistency checks

  • Existing BankCode
  • Existing BrancheCode
  • Consistency of FR nationalCheckDigit

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();  

nvuillam avatar Feb 23 '18 12:02 nvuillam

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();

thecoder8890 avatar May 18 '22 18:05 thecoder8890