Class: PiiScrubber::Detectors::Iban

Inherits:
Base
  • Object
show all
Defined in:
lib/pii_scrubber/detectors/iban.rb

Constant Summary collapse

IBAN_REGEX =

Matches international bank account numbers (15-34 alphanumeric chars with optional spacing/hyphens)

/\b[A-Za-z]{2}\d{2}(?:[ -]?[A-Za-z0-9]{4}){2,7}(?:[ -]?[A-Za-z0-9]{1,4})?\b/

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Constructor Details

#initializeIban

Returns a new instance of Iban.



11
12
13
# File 'lib/pii_scrubber/detectors/iban.rb', line 11

def initialize
  super(name: :iban)
end

Instance Method Details

#patternsObject



15
16
17
# File 'lib/pii_scrubber/detectors/iban.rb', line 15

def patterns
  [IBAN_REGEX]
end

#replace(match, strategy: :placeholder, mask_char: "*", hmac_salt: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pii_scrubber/detectors/iban.rb', line 27

def replace(match, strategy: :placeholder, mask_char: "*", hmac_salt: nil)
  return super unless strategy == :mask

  sanitized = match.gsub(/[\s-]/, "").upcase
  return super if sanitized.length < 8

  prefix = sanitized[0..3] # Country code + check digits
  suffix = sanitized[-4..]
  masked_middle = mask_char * (sanitized.length - 8)
  "#{prefix}#{masked_middle}#{suffix}"
end

#valid?(match) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/pii_scrubber/detectors/iban.rb', line 19

def valid?(match)
  sanitized = match.gsub(/[\s-]/, "").upcase
  return false unless sanitized.length.between?(15, 34)
  return false unless sanitized.match?(/\A[A-Z]{2}\d{2}[A-Z0-9]+\z/)

  modulo97_valid?(sanitized)
end