美文网首页
go判断身份证号是否合法

go判断身份证号是否合法

作者: Feng_Sir | 来源:发表于2018-04-11 11:01 被阅读0次
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]
}

相关文章

网友评论

      本文标题:go判断身份证号是否合法

      本文链接:https://www.haomeiwen.com/subject/wujahftx.html