go判断身份证号是否合法
2018-04-11 本文已影响0人
Feng_Sir
var IdCardCheckSum = []byte{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}
//CheckIdCode 判断身份证号是否合法,返回true合法,返回false不合法
func CheckIdCode(idcode string) bool {
fmt.Println(string(idcode))
length := len(idcode)
if length != 15 && length != 18 {
return false
}
if length == 18 {
var bytes [18]byte
copy(bytes[:], idcode)
chk := checkSum(bytes)
fmt.Println(string(chk))
return (bytes[17] != 'x' && bytes[17] == chk) || (bytes[17] == 'x' && chk == 'X')
}
//默认返回错误
return false
}
func checkSum(idnum [18]byte) byte {
sum := 0
for k, v := range idnum {
sum += (int(v) - 48) * IdCardAlpha[k]
}
i := sum % 11
return IdCardCheckSum[i]
}