Class: PiiScrubber::Detectors::IndianAadhaar

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

Constant Summary collapse

AADHAAR_REGEX =

Matches 12-digit Indian Aadhaar numbers starting with 2-9

/\b[2-9][0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}\b/
D_TABLE =

Verhoeff algorithm multiplication table

[
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
  [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
  [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
  [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
  [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
  [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
  [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
  [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
].freeze
P_TABLE =

Verhoeff algorithm permutation table

[
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
  [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
  [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
  [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
  [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
  [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
  [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
].freeze

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Constructor Details

#initializeIndianAadhaar

Returns a new instance of IndianAadhaar.



37
38
39
# File 'lib/pii_scrubber/detectors/indian_aadhaar.rb', line 37

def initialize
  super(name: :indian_aadhaar)
end

Instance Method Details

#patternsObject



41
42
43
# File 'lib/pii_scrubber/detectors/indian_aadhaar.rb', line 41

def patterns
  [AADHAAR_REGEX]
end

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



55
56
57
58
59
60
61
62
63
# File 'lib/pii_scrubber/detectors/indian_aadhaar.rb', line 55

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

  digits = match.scan(/\d/)
  return super if digits.size != 12

  last_four = digits.last(4).join
  "#{mask_char * 4}-#{mask_char * 4}-#{last_four}"
end

#valid?(match) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
# File 'lib/pii_scrubber/detectors/indian_aadhaar.rb', line 45

def valid?(match)
  digits = match.scan(/\d/).map(&:to_i)
  return false unless digits.size == 12

  # First digit cannot be 0 or 1
  return false if [0, 1].include?(digits.first)

  verhoeff_valid?(digits)
end