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.
 
 

33 lines
652 B

  1. package id_validator
  2. import (
  3. "math"
  4. "strconv"
  5. "strings"
  6. )
  7. func GeneratorCheckBit(body string) string {
  8. // 位置加权
  9. var posWeight [19]float64
  10. for i := 2; i < 19; i++ {
  11. weight := int(math.Pow(2, float64(i-1))) % 11
  12. posWeight[i] = float64(weight)
  13. }
  14. // 累身份证号 body 部分与位置加权的积
  15. bodySum := 0
  16. bodyArray := strings.Split(body, "")
  17. count := len(bodyArray)
  18. for i := 0; i < count; i++ {
  19. bodySubStr, _ := strconv.Atoi(bodyArray[i])
  20. bodySum += bodySubStr * int(posWeight[18-i])
  21. }
  22. // 生成校验码
  23. checkBit := (12 - (bodySum % 11)) % 11
  24. if checkBit == 10 {
  25. return "X"
  26. }
  27. return strconv.Itoa(checkBit)
  28. }