Skip to content

Instantly share code, notes, and snippets.

@douo
Last active September 27, 2018 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douo/afccff7426a5336a2103 to your computer and use it in GitHub Desktop.
Save douo/afccff7426a5336a2103 to your computer and use it in GitHub Desktop.
Chinese ID card number checksum
module ChineseId
WEIGHT = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
CODE = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"]
#S = Sum(Ai * Wi)
#Y = mod(S, 11)
#C = Cy
def self.checksum id
if "String" == id.class.name
number = id.split(//).map{|c| c.to_i}
else
number = id
end
sum = 0
number[0..16].zip(WEIGHT) do |x,y|
sum += x * y
end
y = sum % 11
CODE[y]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment