Skip to content

Instantly share code, notes, and snippets.

@woodrow
Created August 26, 2013 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save woodrow/6347926 to your computer and use it in GitHub Desktop.
Save woodrow/6347926 to your computer and use it in GitHub Desktop.
Passport MRZ check digit algorithm
def compute_check_digit(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
values.zip([7,3,1].cycle).map{|(v,w)| v * w}.reduce(:+) % 10
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment