order_client.go 1.1 KB
package socialwork_sdk

import (
	"encoding/json"
	"gitlab.workai.com.cn/chenang/socialwork-sdk/common"
	"log"
)

type OrderClient struct {
	Key                string `json:"key"`
	IV                 string `json:"iv"`
	PublicKey          string `json:"public_key"`
	CustomerPrivateKey string `json:"customer_private_key"`
}

func NewOrderClient(key, iv, customerPrivateKey, publicKey string) *OrderClient {
	return &OrderClient{
		Key:                key,
		IV:                 iv,
		PublicKey:          publicKey,
		CustomerPrivateKey: customerPrivateKey,
	}
}

func (o *OrderClient) EncryptAndSign(res interface{}) (string, string, error) {
	bs, _ := json.Marshal(res)
	cipherText, err := common.Aes_CBC_Encrypt(bs, []byte(o.Key), []byte(o.IV))
	if err != nil {
		log.Printf("Aes_CBC_Encrypt.failed.err=%v\n", err)
		return "", "", err
	}

	sign, err := common.RsaSignWithMd5Hex(cipherText, o.CustomerPrivateKey)
	if err != nil {
		log.Printf("RsaSignWithMd5Hex.failed.err=%v\n", err)
		return "", "", err
	}
	return cipherText, sign, nil
}

func (o *OrderClient) PrintVersion() string {
	return "1.0"
}