check.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package util
  4. import (
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. var (
  10. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  11. idCertVerifyCode = []byte("10X98765432")
  12. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  13. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  14. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  15. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  16. 'W': 28, 'X': 29, 'Y': 30}
  17. socialCreditMapKeys = []int{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'}
  18. socialCreditMapValues = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
  19. )
  20. //单位证件
  21. func CheckSocialCode(str string) (res bool) {
  22. defer func() {
  23. if r := recover(); r != nil {
  24. res = false
  25. }
  26. }()
  27. socialCode := strings.ToUpper(str)
  28. if len(socialCode) != 18 {
  29. return false
  30. }
  31. check := 0
  32. lastLetter := 0
  33. for index, value := range []byte(socialCode) {
  34. if index == 17 {
  35. lastLetter = int(value)
  36. break
  37. }
  38. if value >= 48 && value <= 57 {
  39. //fmt.Println((int(value)-48),socialCreditMatrix[index])
  40. check = check + (int(value)-48)*socialCreditMatrix[index]
  41. } else {
  42. check = check + socialCreditMap[int(value)]*socialCreditMatrix[index]
  43. }
  44. }
  45. diff := 31 - check%31
  46. keys := make([]int, 0)
  47. values := make([]int, 0)
  48. for k, v := range socialCreditMap {
  49. keys = append(keys, k)
  50. values = append(values, v)
  51. }
  52. verifyCode := socialCreditMapKeys[socialCreditMapValues[diff]]
  53. if verifyCode == lastLetter {
  54. return true
  55. } else {
  56. return false
  57. }
  58. }
  59. //身份证
  60. func CheckIDCert(str string) (res bool) {
  61. defer func() {
  62. if r := recover(); r != nil {
  63. res = false
  64. }
  65. }()
  66. idCert := strings.ToUpper(str)
  67. if len(idCert) != 18 {
  68. return false
  69. }
  70. year, err := strconv.Atoi(idCert[6:10])
  71. if err != nil {
  72. return false
  73. }
  74. if !(1900 < year && year < 2100) {
  75. return false
  76. }
  77. check := 0
  78. lastLetter := 0
  79. for index, value := range []byte(idCert) {
  80. if index == 17 {
  81. lastLetter = int(value)
  82. break
  83. }
  84. if !(value >= 48 && value <= 57) {
  85. return false
  86. }
  87. v := value - 48
  88. check = check + idCertMatrix[index]*int(v)
  89. }
  90. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  91. return false
  92. }
  93. timeLayout := "20060102" //转化所需模板
  94. loc, _ := time.LoadLocation("Local") //重要:获取时区
  95. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  96. if err != nil {
  97. return false
  98. }
  99. verifyCode := int(idCertVerifyCode[check%11])
  100. if lastLetter == verifyCode {
  101. return true
  102. }
  103. return false
  104. }