You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

101 lines
2.3 KiB

  1. package idvalidator
  2. import (
  3. "errors"
  4. "id-validator/data"
  5. "strconv"
  6. )
  7. // 验证身份证号合法性
  8. func IsValid(id string) bool {
  9. code, err := GenerateCode(id)
  10. if err != nil {
  11. return false
  12. }
  13. // 检查顺序码、生日码、地址码
  14. if !CheckOrderCode(code["order"]) || !CheckBirthdayCode(code["birthdayCode"]) || !CheckAddressCode(code["addressCode"], code["birthdayCode"]) {
  15. return false
  16. }
  17. // 15位身份证不含校验码
  18. if code["type"] == "15" {
  19. return true
  20. }
  21. // 校验码
  22. return code["checkBit"] == GeneratorCheckBit(code["body"])
  23. }
  24. // 获取身份证信息
  25. func GetInfo(id string) map[string]string {
  26. // 验证有效性
  27. if !IsValid(id) {
  28. return map[string]string{}
  29. }
  30. code, _ := GenerateCode(id)
  31. addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"])
  32. // fmt.Println(addressInfo)
  33. address, _ := strconv.Atoi(code["addressCode"])
  34. abandoned := "0"
  35. if data.AddressCode[address] == "" {
  36. abandoned = "1"
  37. }
  38. // birthday, _ := time.Parse("20060102", code["birthdayCode"])
  39. sex := "1"
  40. sexCode, _ := strconv.Atoi(code["order"])
  41. if (sexCode % 2) == 0 {
  42. sex = "0"
  43. }
  44. info := map[string]string{
  45. "addressCode": code["addressCode"],
  46. "abandoned": abandoned,
  47. "address": addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
  48. // "addressTree": addressInfo,
  49. // "birthdayCode": birthday,
  50. "constellation": GetConstellation(code["birthdayCode"]),
  51. "chineseZodiac": GetChineseZodiac(code["birthdayCode"]),
  52. "sex": sex,
  53. "length": code["type"],
  54. "checkBit": code["checkBit"],
  55. }
  56. return info
  57. }
  58. // 生成假身份证号码
  59. func Fake(isEighteen bool, address string, birthday string, sex int) string {
  60. // 生成地址码
  61. addressCode := GeneratorAddressCode(address)
  62. // 出生日期码
  63. birthdayCode := GeneratorBirthdayCode(birthday)
  64. // 生成顺序码
  65. orderCode := GeneratorOrderCode(sex)
  66. if !isEighteen {
  67. return addressCode + Substr(birthdayCode, 2, 6) + orderCode
  68. }
  69. body := addressCode + birthdayCode + orderCode
  70. return body + GeneratorCheckBit(body)
  71. }
  72. // 15位升级18位号码
  73. func Upgrade(id string) (string, error) {
  74. if !IsValid(id) {
  75. return "", errors.New("Not Valid ID card number.")
  76. }
  77. code, _ := GenerateShortCode(id)
  78. body := code["addressCode"] + code["birthdayCode"] + code["order"]
  79. return body + GeneratorCheckBit(body), nil
  80. }