正在显示
6 个修改的文件
包含
202 行增加
和
13 行删除
| 1 | -package common | 1 | +package socialwork_sdk |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| @@ -7,7 +7,7 @@ import ( | @@ -7,7 +7,7 @@ import ( | ||
| 7 | "encoding/base64" | 7 | "encoding/base64" |
| 8 | ) | 8 | ) |
| 9 | 9 | ||
| 10 | -func Aes_CBC_Decrypt(crypted []byte, key, iv []byte) ([]byte, error) { | 10 | +func aes_CBC_Decrypt(crypted []byte, key, iv []byte) ([]byte, error) { |
| 11 | block, err := aes.NewCipher(key) | 11 | block, err := aes.NewCipher(key) |
| 12 | if err != nil { | 12 | if err != nil { |
| 13 | return nil, err | 13 | return nil, err |
| @@ -26,7 +26,7 @@ func unpadding(cipherText []byte) []byte { | @@ -26,7 +26,7 @@ func unpadding(cipherText []byte) []byte { | ||
| 26 | return cipherText | 26 | return cipherText |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | -func Aes_CBC_Encrypt(data []byte, key, iv []byte) (string, error) { | 29 | +func aes_CBC_Encrypt(data []byte, key, iv []byte) (string, error) { |
| 30 | block, err := aes.NewCipher(key) | 30 | block, err := aes.NewCipher(key) |
| 31 | if err != nil { | 31 | if err != nil { |
| 32 | return "", err | 32 | return "", err |
constant.go
0 → 100644
httpclient.go
0 → 100644
| 1 | +package socialwork_sdk | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "bytes" | ||
| 5 | + "encoding/json" | ||
| 6 | + "errors" | ||
| 7 | + "fmt" | ||
| 8 | + "io" | ||
| 9 | + "io/ioutil" | ||
| 10 | + "net/http" | ||
| 11 | + "net/http/cookiejar" | ||
| 12 | + "strings" | ||
| 13 | +) | ||
| 14 | + | ||
| 15 | +const ( | ||
| 16 | + post = "POST" | ||
| 17 | + get = "GET" | ||
| 18 | + put = "PUT" | ||
| 19 | +) | ||
| 20 | + | ||
| 21 | +func makeHttpRequestV4(method, url string, headers map[string]string, entity map[string]interface{}, jar *cookiejar.Jar) (string, int, error) { | ||
| 22 | + var body io.Reader | ||
| 23 | + var err error | ||
| 24 | + | ||
| 25 | + if entity != nil { | ||
| 26 | + switch method { | ||
| 27 | + case post, put: | ||
| 28 | + if len(entity) == 1 && entity["jsonbody"] != nil { | ||
| 29 | + jsonBody, ok := entity["jsonbody"].(string) | ||
| 30 | + if ok { | ||
| 31 | + body = bytes.NewBuffer([]byte(jsonBody)) | ||
| 32 | + } | ||
| 33 | + } else { | ||
| 34 | + b, err := json.Marshal(entity) | ||
| 35 | + if err != nil { | ||
| 36 | + return "", 0, err | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1) | ||
| 40 | + b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1) | ||
| 41 | + b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1) | ||
| 42 | + | ||
| 43 | + body = bytes.NewBuffer(b) | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + case get: | ||
| 47 | + if len(entity) > 0 { | ||
| 48 | + params := make([]string, len(entity)) | ||
| 49 | + index := 0 | ||
| 50 | + for k, v := range entity { | ||
| 51 | + _v := fmt.Sprintf("%v", v) | ||
| 52 | + params[index] = fmt.Sprintf("%s=%v", k, _v) | ||
| 53 | + index++ | ||
| 54 | + } | ||
| 55 | + queryStr := strings.Join(params, "&") | ||
| 56 | + url = fmt.Sprintf("%s?%s", url, queryStr) | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + req, err := http.NewRequest(method, url, body) | ||
| 62 | + if err != nil { | ||
| 63 | + return "", 0, err | ||
| 64 | + } | ||
| 65 | + // set the http request header | ||
| 66 | + for key, value := range headers { | ||
| 67 | + req.Header.Set(key, value) | ||
| 68 | + } | ||
| 69 | + if entity != nil && (method == post || method == put) { | ||
| 70 | + req.Header.Set("Content-Type", "application/json;charset=utf-8") | ||
| 71 | + req.Header.Set("Accept", "application/json") | ||
| 72 | + } | ||
| 73 | + req.Header.Add("Connection", "close") | ||
| 74 | + req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5") | ||
| 75 | + | ||
| 76 | + //跳过证书验证 | ||
| 77 | + // tr := &http.Transport{ | ||
| 78 | + // TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
| 79 | + // } | ||
| 80 | + client := http.DefaultClient | ||
| 81 | + if jar != nil { | ||
| 82 | + client = &http.Client{ | ||
| 83 | + Jar: jar, | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + // client.Transport = tr | ||
| 87 | + | ||
| 88 | + fmt.Printf("xuht================Request: %+v\n", *req) | ||
| 89 | + res, err := client.Do(req) | ||
| 90 | + if err != nil { | ||
| 91 | + fmt.Println("faild to do the request with error ", err) | ||
| 92 | + return "", 0, err | ||
| 93 | + } | ||
| 94 | + defer res.Body.Close() | ||
| 95 | + | ||
| 96 | + if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusNoContent { | ||
| 97 | + resBody, _ := ioutil.ReadAll(res.Body) | ||
| 98 | + fmt.Println("code is not 200 ", res.StatusCode, string(resBody)) | ||
| 99 | + return "", 0, errors.New("http request failed to call") | ||
| 100 | + } | ||
| 101 | + resBody, err := ioutil.ReadAll(res.Body) | ||
| 102 | + if err != nil { | ||
| 103 | + fmt.Println("could not read the response body") | ||
| 104 | + return "", 0, errors.New("the response could not be read") | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + return string(resBody), res.StatusCode, nil | ||
| 108 | +} |
model/order_client.go
0 → 100644
| 1 | package socialwork_sdk | 1 | package socialwork_sdk |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "encoding/base64" | ||
| 4 | "encoding/json" | 5 | "encoding/json" |
| 5 | - "gitlab.workai.com.cn/chenang/socialwork-sdk/common" | 6 | + "gitlab.workai.com.cn/chenang/socialwork-sdk/model" |
| 6 | "log" | 7 | "log" |
| 7 | ) | 8 | ) |
| 8 | 9 | ||
| @@ -11,26 +12,28 @@ type OrderClient struct { | @@ -11,26 +12,28 @@ type OrderClient struct { | ||
| 11 | IV string `json:"iv"` | 12 | IV string `json:"iv"` |
| 12 | PublicKey string `json:"public_key"` | 13 | PublicKey string `json:"public_key"` |
| 13 | CustomerPrivateKey string `json:"customer_private_key"` | 14 | CustomerPrivateKey string `json:"customer_private_key"` |
| 15 | + PtCode string `json:"pt_code"` | ||
| 16 | + IP string `json:"ip"` | ||
| 14 | } | 17 | } |
| 15 | 18 | ||
| 16 | -func NewOrderClient(key, iv, customerPrivateKey, publicKey string) *OrderClient { | 19 | +func NewOrderClient(key, iv, customerPrivateKey, publicKey, ip string) *OrderClient { |
| 17 | return &OrderClient{ | 20 | return &OrderClient{ |
| 18 | Key: key, | 21 | Key: key, |
| 19 | IV: iv, | 22 | IV: iv, |
| 20 | PublicKey: publicKey, | 23 | PublicKey: publicKey, |
| 21 | CustomerPrivateKey: customerPrivateKey, | 24 | CustomerPrivateKey: customerPrivateKey, |
| 25 | + IP: ip, | ||
| 22 | } | 26 | } |
| 23 | } | 27 | } |
| 24 | 28 | ||
| 25 | -func (o *OrderClient) EncryptAndSign(res interface{}) (string, string, error) { | ||
| 26 | - bs, _ := json.Marshal(res) | ||
| 27 | - cipherText, err := common.Aes_CBC_Encrypt(bs, []byte(o.Key), []byte(o.IV)) | 29 | +func (o *OrderClient) EncryptAndSign(bs []byte) (string, string, error) { |
| 30 | + cipherText, err := aes_CBC_Encrypt(bs, []byte(o.Key), []byte(o.IV)) | ||
| 28 | if err != nil { | 31 | if err != nil { |
| 29 | log.Printf("Aes_CBC_Encrypt.failed.err=%v\n", err) | 32 | log.Printf("Aes_CBC_Encrypt.failed.err=%v\n", err) |
| 30 | return "", "", err | 33 | return "", "", err |
| 31 | } | 34 | } |
| 32 | 35 | ||
| 33 | - sign, err := common.RsaSignWithMd5Hex(cipherText, o.CustomerPrivateKey) | 36 | + sign, err := rsaSignWithMd5Hex(cipherText, o.CustomerPrivateKey) |
| 34 | if err != nil { | 37 | if err != nil { |
| 35 | log.Printf("RsaSignWithMd5Hex.failed.err=%v\n", err) | 38 | log.Printf("RsaSignWithMd5Hex.failed.err=%v\n", err) |
| 36 | return "", "", err | 39 | return "", "", err |
| @@ -38,6 +41,52 @@ func (o *OrderClient) EncryptAndSign(res interface{}) (string, string, error) { | @@ -38,6 +41,52 @@ func (o *OrderClient) EncryptAndSign(res interface{}) (string, string, error) { | ||
| 38 | return cipherText, sign, nil | 41 | return cipherText, sign, nil |
| 39 | } | 42 | } |
| 40 | 43 | ||
| 41 | -func (o *OrderClient) PrintVersion() string { | ||
| 42 | - return "1.0" | 44 | +func (o *OrderClient) DecryptAndVerySign(cipherText, sign string) ([]byte, error) { |
| 45 | + _cipherText, err := base64.StdEncoding.DecodeString(cipherText) | ||
| 46 | + if err != nil { | ||
| 47 | + return nil, err | ||
| 48 | + } | ||
| 49 | + err = rsaVerifySignWithMd5Base64(cipherText, sign, o.PublicKey) | ||
| 50 | + if err != nil { | ||
| 51 | + return nil, err | ||
| 52 | + } | ||
| 53 | + content, err := aes_CBC_Decrypt(_cipherText, []byte(o.Key), []byte(o.IV)) | ||
| 54 | + if err != nil { | ||
| 55 | + return nil, err | ||
| 56 | + } | ||
| 57 | + return content, nil | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +func (o *OrderClient) SendRequest(businessCode, reqBody string) (string, error) { | ||
| 61 | + //校验参数 reqbody | ||
| 62 | + checkErr := o.checkParams(businessCode, reqBody) | ||
| 63 | + if checkErr != nil { | ||
| 64 | + return "", checkErr | ||
| 65 | + } | ||
| 66 | + cipherText, sign, err := o.EncryptAndSign([]byte(reqBody)) | ||
| 67 | + if err != nil { | ||
| 68 | + return "", err | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + orderModel := model.OrderModel{ | ||
| 72 | + Data: cipherText, | ||
| 73 | + Sign: sign, | ||
| 74 | + PtCode: o.PtCode, | ||
| 75 | + BusinessCode: businessCode, | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + _orderModel, err := json.Marshal(orderModel) | ||
| 79 | + if err != nil { | ||
| 80 | + return "", err | ||
| 81 | + } | ||
| 82 | + respBody, _, err := makeHttpRequestV4(post, o.IP+orderUrl, nil, map[string]interface{}{"jsonbody": string(_orderModel)}, nil) | ||
| 83 | + if err != nil { | ||
| 84 | + return "", err | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + return respBody, nil | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +func (o *OrderClient) checkParams(businessCode, reqBody string) error { | ||
| 91 | + return nil | ||
| 43 | } | 92 | } |
| 1 | -package common | 1 | +package socialwork_sdk |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "crypto" | 4 | "crypto" |
| @@ -10,7 +10,7 @@ import ( | @@ -10,7 +10,7 @@ import ( | ||
| 10 | "encoding/pem" | 10 | "encoding/pem" |
| 11 | ) | 11 | ) |
| 12 | 12 | ||
| 13 | -func RsaSignWithMd5Hex(data string, prvKey string) (string, error) { | 13 | +func rsaSignWithMd5Hex(data string, prvKey string) (string, error) { |
| 14 | block, _ := pem.Decode([]byte(prvKey)) | 14 | block, _ := pem.Decode([]byte(prvKey)) |
| 15 | privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) | 15 | privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes) |
| 16 | if err != nil { | 16 | if err != nil { |
| @@ -29,3 +29,22 @@ func RsaSignWithMd5Hex(data string, prvKey string) (string, error) { | @@ -29,3 +29,22 @@ func RsaSignWithMd5Hex(data string, prvKey string) (string, error) { | ||
| 29 | out := base64.StdEncoding.EncodeToString(signature) | 29 | out := base64.StdEncoding.EncodeToString(signature) |
| 30 | return out, nil | 30 | return out, nil |
| 31 | } | 31 | } |
| 32 | + | ||
| 33 | +func rsaVerifySignWithMd5Base64(originalData, signData, pubKey string) error { | ||
| 34 | + sign, err := base64.StdEncoding.DecodeString(signData) | ||
| 35 | + if err != nil { | ||
| 36 | + return err | ||
| 37 | + } | ||
| 38 | + block, _ := pem.Decode([]byte(pubKey)) | ||
| 39 | + pub, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
| 40 | + if err != nil { | ||
| 41 | + return err | ||
| 42 | + } | ||
| 43 | + hash := md5.New() | ||
| 44 | + hash.Write([]byte(originalData)) | ||
| 45 | + err = rsa.VerifyPKCS1v15(pub.(*rsa.PublicKey), crypto.MD5, hash.Sum(nil), sign) | ||
| 46 | + if err != nil { | ||
| 47 | + return err | ||
| 48 | + } | ||
| 49 | + return nil | ||
| 50 | +} |
请
注册
或
登录
后发表评论