Created
July 27, 2013 18:25
-
-
Save libbymiller/6095798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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