cdbd.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "gd_vehicle/common.in/utils"
  9. "go.uber.org/zap"
  10. "gd_vehicle/common.in/config"
  11. id2 "gd_vehicle/common.in/id"
  12. //"github.com/tidwall/gjson"
  13. "io/ioutil"
  14. "net/http"
  15. "time"
  16. "github.com/tjfoc/gmsm/sm4"
  17. )
  18. // PKCSPadd填充算法
  19. func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
  20. padding := blockSize - len(ciphertext)%blockSize
  21. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  22. return append(ciphertext, padText...)
  23. }
  24. func PKCS7UnPadding(origData []byte) []byte {
  25. length := len(origData)
  26. unPadding := int(origData[length-1])
  27. return origData[:(length - unPadding)]
  28. }
  29. func SM4ECBBase64Encrypt(originalText string, key []byte) (string, error) {
  30. block, err := sm4.NewCipher(key)
  31. if err != nil {
  32. panic(err)
  33. }
  34. originalBytes := []byte(originalText)
  35. originalBytes = PKCS7Padding(originalBytes, block.BlockSize())
  36. cipherArr := make([]byte, 0)
  37. cArr := make([]byte, 16)
  38. j := 0
  39. for i := 0; i < len(originalBytes)/16; i++ {
  40. original := originalBytes[j : j+16]
  41. block.Encrypt(cArr, original)
  42. cipherArr = append(cipherArr, cArr...)
  43. j = j + 16
  44. }
  45. base64Str := base64.StdEncoding.EncodeToString(cipherArr)
  46. return base64Str, nil
  47. }
  48. func SM4ECBBase64Decrypt(cipherText string) (string, error) {
  49. key, _ := hex.DecodeString(config.Conf.ThirdPart.CdbdSecret)
  50. cInArr, _ := base64.StdEncoding.DecodeString(cipherText)
  51. block, err := sm4.NewCipher(key)
  52. if err != nil {
  53. panic(err)
  54. }
  55. cipherArr := make([]byte, 0)
  56. cArr := make([]byte, 16)
  57. j := 0
  58. for i := 0; i < len(cInArr)/16; i++ {
  59. original := cInArr[j : j+16]
  60. block.Decrypt(cArr, original)
  61. cipherArr = append(cipherArr, cArr...)
  62. j = j + 16
  63. }
  64. originalText := string(PKCS7UnPadding(cipherArr))
  65. return originalText, nil
  66. }
  67. func CdBdClientPost(api, code string, data map[string]string, httpTimeout int) (result []byte, err error) {
  68. defer func() {
  69. l.Info("thirdparty",
  70. zap.String("api", api),
  71. zap.String("request", utils.MarshalJsonString(data)),
  72. zap.String("response", utils.MarshalJsonString(result)))
  73. }()
  74. if httpTimeout == 0 {
  75. httpTimeout = 30
  76. }
  77. key, _ := hex.DecodeString(config.Conf.ThirdPart.CdbdSecret)
  78. client := &http.Client{}
  79. client.Timeout = time.Duration(httpTimeout) * time.Second
  80. authorization, _ := id2.GetDistributedUniqueID()
  81. data["authorization"] = fmt.Sprintf("%d", authorization)
  82. //fmt.Println("data:", data)
  83. param := map[string]string{}
  84. rdata, _ := json.Marshal(data)
  85. //fmt.Println("rdata :", string(rdata), data)
  86. tmp, err := SM4ECBBase64Encrypt(string(rdata), key)
  87. if err != nil {
  88. fmt.Println("encoding failed :", tmp, err)
  89. }
  90. param["param"] = tmp
  91. requestData, _ := json.Marshal(param)
  92. //fmt.Println("request data :", string(requestData))
  93. req, err := http.NewRequest("POST", api, bytes.NewBuffer(requestData))
  94. if err != nil {
  95. return nil, err
  96. }
  97. timestamp := time.Now().UnixNano() / 1e6
  98. id, _ := id2.GetDistributedUniqueID()
  99. header := map[string]string{}
  100. header["random"] = fmt.Sprintf("%d", id)
  101. header["timestamp"] = fmt.Sprintf("%d", timestamp)
  102. header["clientId"] = config.Conf.ThirdPart.CdbdClientId //"f830bebbec534450b22d2e766cb4cc36"
  103. header["code"] = code //"DPA12Y003R02310231-DSA12CDS0231"
  104. siginStr := ""
  105. for k, v := range header {
  106. tmp := k + "=" + v
  107. if siginStr == "" {
  108. siginStr = tmp
  109. } else {
  110. siginStr = siginStr + "&" + tmp
  111. }
  112. }
  113. sigin, _ := SM4ECBBase64Encrypt(siginStr, key)
  114. header["signature"] = sigin
  115. //fmt.Println("header :", header, siginStr)
  116. for k, v := range header {
  117. req.Header.Set(k, v)
  118. }
  119. req.Header.Set("Content-Type", "application/json")
  120. resp, err := client.Do(req)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if resp.StatusCode != http.StatusOK {
  125. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  126. }
  127. defer resp.Body.Close()
  128. result, err = ioutil.ReadAll(resp.Body)
  129. return
  130. }