Skip to content

Commit

Permalink
Fixes iban checksum calculation (#2591)
Browse files Browse the repository at this point in the history
* Fixes iban checksum calculation

* Revisions: Refines wiki comment; Updates test helper method name

* Adds iban_checksum test
  • Loading branch information
srcoley authored Oct 31, 2022
1 parent 1844884 commit 5bcdec9
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/faker/default/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ def compile_bsb_number
end

# Calculates the mandatory checksum in 3rd and 4th characters in IBAN format
# source: https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN
# source: https://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits
def iban_checksum(country_code, account)
# Converts letters to numbers according the iban rules, A=10..Z=35
account_to_number = "#{account}#{country_code}00".upcase.chars.map do |d|
d =~ /[A-Z]/ ? (d.ord - 55).to_s : d
end.join.to_i

# This is answer to (iban_to_num + checksum) % 97 == 1
checksum = (1 - account_to_number) % 97
# This is the correct answer to (iban_to_num + checksum) % 97 == 1
checksum = 98 - (account_to_number % 97)

# Use leftpad to make the size always to 2
checksum.to_s.rjust(2, '0')
Expand Down
Loading

0 comments on commit 5bcdec9

Please sign in to comment.