Class: MRZ::CheckDigit

Inherits:
Object
  • Object
show all
Defined in:
lib/verified/parser/mrz/check_digit.rb

Instance Method Summary collapse

Instance Method Details

#check_calc(str) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/verified/parser/mrz/check_digit.rb', line 3

def check_calc(str)
  str = str.strip.upcase
  values = str.chars.map do |char|
    case char
      when '<'
        0
      when 'A'..'Z'
        char.ord - 65 + 10
      when '0'..'9'
        char.ord - 48
      else
        raise "Unexpected character '#{char}'"
      end
    end
  return (values.zip([7,3,1].cycle).map{|(v,w)| v * w}.reduce(:+) % 10).to_s
end