Sha256: a96ee18b467f9e68d35399b3a1a12a4684da05278c44f5ab6b0147260538eea6

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Fluent::PAN
  class Masker

    CHECKSUM_FUNC = {
      luhn: ->(digits){
        sum = 0
        alt = false
        digits.reverse.each do |i|
          if alt
            i *= 2
            if i > 9
              i -= 9
            end
          end
          sum += i
          alt = !alt
        end
        (sum % 10).zero?
      },
      none: ->digits{
        # Do nothing. always satisfied.
        true
      }
    }

    def initialize(regexp, checksum_algorithm, mask, force=false)
      @regexp = regexp
      @mask = mask
      @force = force
      @checksum_func = CHECKSUM_FUNC[checksum_algorithm]
    end

    def mask_if_found_pan(orgval)
      filtered = orgval.to_s.gsub(@regexp) do |match|
        pan = match.split("").select { |i| i =~ /\d/ }.map { |j| j.to_i }

        if valid?(pan)
          match = @mask
        else
          match
        end
      end

      retval = filtered
      if orgval.is_a? Integer
        if numerals_mask?
          retval = filtered.to_i
        else
          if @force
            retval = filtered
          else
            retval = orgval
          end
        end
      end
      retval
    end

    def valid?(pan)
      @checksum_func.call(pan)
    end

    def numerals_mask?
      if @mask.to_s =~ /^\d+$/
        true
      else
        false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-pan-anonymizer-0.0.1 lib/fluent/plugin/pan/masker.rb