Last active
September 27, 2018 17:48
-
-
Save douo/afccff7426a5336a2103 to your computer and use it in GitHub Desktop.
Chinese ID card number checksum
This file contains hidden or 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
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