Sha256: 6e44a3083aa9a23c0075ef00be1357c886417f116504a4b3e57267907da1557e

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

module MRZ
  class Check
    def initialize
      @@digit_checker = CheckDigit.new
      @@date_converter = YYDate.new
    end
    
    def check(mrz_line_1, mrz_line_2)
      
      if mrz_line_1.length != 44 && mrz_line_2.length != 44
        return false
      end

      # Read the first line without chevrons
      split = mrz_line_1.split(/<+/)

      doc_data = Hash.new(9)
      doc_data["issuing_state"] = mrz_line_1[2...5].sub(/<+/, '')
      doc_data["last_name"] = split[1][3..-1]
      doc_data["first_names"] = split[2..-1]
      doc_data["passport_number"] = mrz_line_2[0...9]
      doc_data["nationality"] = mrz_line_2[10...13].sub(/<+/, '')
      doc_data["date_of_birth"] = @@date_converter.convert_to_date(mrz_line_2[13...19])
      doc_data["gender"] = mrz_line_2[20].sub(/<+/, '')
      doc_data["expiry_date"] = @@date_converter.convert_to_date(mrz_line_2[21...27])
      doc_data["personal_number"] = mrz_line_2[28...42].sub(/<+/, '')

      # Grabbing the MRZ's check digits
      doc_check = Array.new
      doc_check[0] = mrz_line_2[9].to_s
      doc_check[1] = mrz_line_2[19].to_s
      doc_check[2] = mrz_line_2[27].to_s
      doc_check[3] = mrz_line_2[42].to_s
      doc_check[4] = mrz_line_2[43].to_s

      # Calculating our own check digits...
      our_check = Array.new
      our_check[0] = @@digit_checker.check_calc(mrz_line_2[0...9])
      our_check[1] = @@digit_checker.check_calc(mrz_line_2[13...19])
      our_check[2] = @@digit_checker.check_calc(mrz_line_2[21...27])
      our_check[3] = @@digit_checker.check_calc(mrz_line_2[28...42])
      our_check[4]	= @@digit_checker.check_calc(mrz_line_2[0...10]+mrz_line_2[13...20]+mrz_line_2[21...43])

      # The 4th check digit can be either > or 0, we always return 0 from our CheckDigit calc.
      if our_check[3] == "0" && doc_check[3] == "<"
      	our_check[3] = "<"
      end
  		
      return doc_data if doc_check.uniq.sort == our_check.uniq.sort
      return false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
verified-1.3.1 lib/verified/parser/mrz/check.rb