id-validator/id_validator.go

131 lines
2.8 KiB
Go
Raw Normal View History

2021-01-11 17:55:24 +08:00
package idvalidator
2021-01-10 13:29:14 +08:00
2021-01-10 14:56:35 +08:00
import (
2021-01-10 19:22:33 +08:00
"errors"
2021-01-11 11:53:22 +08:00
"strconv"
2021-01-11 18:48:50 +08:00
"time"
"id-validator/data"
2021-01-10 14:56:35 +08:00
)
2021-01-11 18:48:50 +08:00
// 身份证信息
type IdInfo struct {
AddressCode int
Abandoned int
Address string
AddressTree []string
Birthday time.Time
Constellation string
ChineseZodiac string
Sex int
Length int
CheckBit int
}
2021-01-10 13:29:14 +08:00
// 验证身份证号合法性
func IsValid(id string) bool {
2021-01-11 17:55:24 +08:00
code, err := GenerateCode(id)
if err != nil {
2021-01-10 13:29:14 +08:00
return false
}
2021-01-11 17:55:24 +08:00
// 检查顺序码、生日码、地址码
if !CheckOrderCode(code["order"]) || !CheckBirthdayCode(code["birthdayCode"]) || !CheckAddressCode(code["addressCode"], code["birthdayCode"]) {
2021-01-10 13:29:14 +08:00
return false
}
// 15位身份证不含校验码
if code["type"] == "15" {
return true
}
2021-01-11 17:55:24 +08:00
// 校验码
return code["checkBit"] == GeneratorCheckBit(code["body"])
2021-01-10 13:29:14 +08:00
}
2021-01-10 14:56:35 +08:00
// 获取身份证信息
2021-01-11 18:48:50 +08:00
func GetInfo(id string) (IdInfo, error) {
2021-01-10 14:56:35 +08:00
// 验证有效性
if !IsValid(id) {
2021-01-11 18:48:50 +08:00
return IdInfo{}, errors.New("Not Valid ID card number.")
2021-01-10 14:56:35 +08:00
}
2021-01-11 17:55:24 +08:00
code, _ := GenerateCode(id)
2021-01-11 18:48:50 +08:00
addressCode, _ := strconv.Atoi(code["addressCode"])
2021-01-10 14:56:35 +08:00
2021-01-11 18:48:50 +08:00
// 地址信息
2021-01-10 14:56:35 +08:00
addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"])
2021-01-11 18:48:50 +08:00
var addressTree []string
for _, val := range addressInfo {
addressTree = append(addressTree, val)
2021-01-10 14:56:35 +08:00
}
2021-01-11 18:48:50 +08:00
// 是否废弃
var abandoned int
if data.AddressCode[addressCode] == "" {
abandoned = 1
}
// 生日
birthday, _ := time.Parse("20060102", code["birthdayCode"])
// 性别
sex := 1
2021-01-10 14:56:35 +08:00
sexCode, _ := strconv.Atoi(code["order"])
if (sexCode % 2) == 0 {
2021-01-11 18:48:50 +08:00
sex = 0
2021-01-10 14:56:35 +08:00
}
2021-01-11 18:48:50 +08:00
// 长度
length, _ := strconv.Atoi(code["type"])
// Bit码
checkBit, _ := strconv.Atoi(code["checkBit"])
return IdInfo{
AddressCode: addressCode,
Abandoned: abandoned,
Address: addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
AddressTree: addressTree,
Birthday: birthday,
Constellation: GetConstellation(code["birthdayCode"]),
ChineseZodiac: GetChineseZodiac(code["birthdayCode"]),
Sex: sex,
Length: length,
CheckBit: checkBit,
}, nil
2021-01-10 14:56:35 +08:00
}
2021-01-10 19:22:33 +08:00
2021-01-11 17:55:24 +08:00
// 生成假身份证号码
func Fake(isEighteen bool, address string, birthday string, sex int) string {
2021-01-10 19:22:33 +08:00
// 生成地址码
addressCode := GeneratorAddressCode(address)
// 出生日期码
birthdayCode := GeneratorBirthdayCode(birthday)
2021-01-11 17:55:24 +08:00
// 生成顺序码
2021-01-10 19:22:33 +08:00
orderCode := GeneratorOrderCode(sex)
if !isEighteen {
return addressCode + Substr(birthdayCode, 2, 6) + orderCode
}
body := addressCode + birthdayCode + orderCode
return body + GeneratorCheckBit(body)
}
// 15位升级18位号码
2021-01-11 17:55:24 +08:00
func Upgrade(id string) (string, error) {
2021-01-10 19:22:33 +08:00
if !IsValid(id) {
return "", errors.New("Not Valid ID card number.")
}
2021-01-11 17:55:24 +08:00
code, _ := GenerateShortCode(id)
2021-01-10 19:22:33 +08:00
body := code["addressCode"] + code["birthdayCode"] + code["order"]
return body + GeneratorCheckBit(body), nil
}