Create a gist now

Instantly share code, notes, and snippets.

# see http://en.wikipedia.org/wiki/Machine-readable_passport
def checksum(str)
arr = str.split("")
count = 1
result = 0
arr.each do |n|
num = nil
factor = 0
if(count%3==1)
factor = 7
num = n.to_i*factor
end
if(count%3==2)
factor = 3
num = n.to_i*factor
end
if(count%3==0)
factor = 1
num = n.to_i*factor
end
count = count + 1
result = result + num
end
return result%10
end
if(!ARGV || ARGV.length<3 || ARGV[0].length != 9 || ARGV[1].length != 6 || ARGV[2].length !=6)
puts "usage: ruby passport.rb 123456789 YYMMDD YYMMDD"
else
first_checksum = checksum(ARGV[0])
second_checksum = checksum(ARGV[1])
third_checksum = checksum(ARGV[2])
final_checksum = checksum("#{ARGV[0]}#{first_checksum}#{ARGV[1]}#{second_checksum}#{ARGV[2]}#{third_checksum}")
str = "#{ARGV[0]}#{first_checksum}GBR#{ARGV[1]}#{second_checksum}<#{ARGV[2]}#{third_checksum}<<<<<<<<<<<<<<0#{final_checksum}"
puts str
puts str.length
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment