123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package thirdparty
- import (
- "bytes"
- "crypto"
- "crypto/rand"
- "crypto/rsa"
- "crypto/x509"
- "gd_management/common.in/utils"
- "encoding/base64"
- "encoding/json"
- "encoding/pem"
- "errors"
- "go.uber.org/zap"
- "io/ioutil"
- "net/http"
- "time"
- )
- var applicationId = "TINYTIMES_SERVICE"
- var publicKey = []byte(`
- -----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCakL0HbEbh3I/WS/BnLjfohhDt+n3ITxHy5mc/
- GN4fbWUEBM419VJz7qwelrNSuFsKJl057h9yGzQ4wYHaJCpQZaLYFqJXJYqC9nNtD0KM7c7J08qs
- i24Nx+qKDtLTWH6pK5Qc+aWlF7s4LWNyCb/STJv7f6G+Q/qtVji1fNW2aQIDAQAB
- -----END PUBLIC KEY-----
- `)
- var privateKey = []byte(`
- -----BEGIN RSA PRIVATE KEY-----
- MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJqQvQdsRuHcj9ZL8GcuN+iGEO36
- fchPEfLmZz8Y3h9tZQQEzjX1UnPurB6Ws1K4WwomXTnuH3IbNDjBgdokKlBlotgWolclioL2c20P
- QoztzsnTyqyLbg3H6ooO0tNYfqkrlBz5paUXuzgtY3IJv9JMm/t/ob5D+q1WOLV81bZpAgMBAAEC
- gYEAlOVLg8QVwGy2D21WKD+I0jGcS3RcCUPKeaDk4w60o7fBcQ6kC6zJAi9wlYL9OnmoEe4FLtIq
- bsSwGOCok5j1PL9jHBLfdO3yojgMKbTj/3n3Wh5fipTL06bJhWsDbTo3P0EjfbNZlj8A6tQmlUSk
- h7UIBIEfVkivwSpwMwllnoUCQQDWjOqM6Ds4iwGPxGVakM/S5gl/M5OEcXddPo5ac7KToUIy99Yg
- /lzxrY6G+ZEU3zWEm6lt3Pzdkuj24UJNikXvAkEAuG0fjj7/g+mH96NKxSeKhBQeUiBPCJc8niPu
- ax+6aWyZqQeGunSILXxC+pm0eA8ukOEvRPiJCU/jYCrV7ZLhJwJAEAgNiqPbsPjqD/UL4b2KWebo
- dMHE1xOnaKdA+c9FW7Sj/qz4KhT3mvNt//Xr+lRHanY7LcNifaf5kmD4lFStrwJAIvNrO4Xs3o7v
- WZr0nJB1y7oUVPQShkpDDqU4lcyZg0k1hbcLbWKQPC6EcwGWFfqqlIJYdvWy4rTlHmB/0lo3mQJB
- AK4cdZIWBOII7KtVn20NTs5COtcFMyz9ztmjxEMD1vkPH9fJ66YcVz6GWKVo4vBHopbNUXkqpH6v
- DIR3C+LXw8M=
- -----END RSA PRIVATE KEY-----
- `)
- type ZtData struct {
- SendTime string `json:"sendTime"`
- Sign string `json:"sign"`
- Msg string `json:"msg"`
- Data map[string]string `json:"data"`
- }
- func GenPrivateKey(privateKey []byte) (*rsa.PrivateKey, error) {
- var err error
- var block *pem.Block
- block, _ = pem.Decode(privateKey)
- if block == nil {
- return nil, errors.New("私钥错误")
- }
- prkI, err := x509.ParsePKCS8PrivateKey(block.Bytes)
- x509.ParsePKCS1PrivateKey(block.Bytes)
- if err != nil {
- return nil, errors.New("解析私钥错误")
- }
- pri := prkI.(*rsa.PrivateKey)
- return pri, nil
- }
- func SignPKCS1v15(src, privateKey []byte, hash crypto.Hash) ([]byte, error) {
- h := hash.New()
- h.Write(src)
- var hashed = h.Sum(nil)
- pri, err := GenPrivateKey(privateKey)
- if err != nil {
- return nil, err
- }
- ret, err := rsa.SignPKCS1v15(rand.Reader, pri, hash, hashed)
- if err != nil {
- return nil, errors.New("签名错误")
- }
- return ret, err
- }
- func signMd5(data []byte) (s string, err error) {
- sig, err := SignPKCS1v15(data, privateKey, crypto.MD5)
- if err != nil {
- return "", err
- }
- s = base64.StdEncoding.EncodeToString(sig)
- return s, nil
- }
- func ztFullUrl(host, api string) string {
- return host + "/" + api
- }
- func ZtHttpPost(api string, data map[string]string) (result []byte, err error) {
- //fullApi := ztFullUrl(host, api)
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", utils.MarshalJsonString(result)))
- }()
- if data == nil {
- data = make(map[string]string, 0)
- }
- data["applicationID"] = applicationId
- // 解析待签名的数据
- jsonData, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
- // Md5WithRsa 签名
- ret, err := signMd5(jsonData)
- if err != nil {
- return nil, err
- }
- var ztD ZtData
- var timeLayout = "2006-01-02 15:04:05"
- timeNow := time.Now().Format(timeLayout)
- ztD.SendTime = timeNow
- ztD.Sign = ret
- ztD.Data = data
- ztData, err := json.Marshal(ztD)
- if err != nil {
- return nil, err
- }
- client := &http.Client{}
- client.Timeout = 10 * time.Second
- req, err := http.NewRequest("POST", api, bytes.NewBuffer(ztData))
- if err != nil {
- return nil, err
- }
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- result, err = ioutil.ReadAll(resp.Body)
- return result, err
- }
|