package supplierrpc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.tetele.net/tgo/crypter"
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
func SetResData(data interface{}, res *Params) {
|
|
res_data_json, err := json.Marshal(data)
|
|
if err == nil {
|
|
|
|
encryData := crypter.DesEn(string(res_data_json), DES_KEY)
|
|
|
|
now_str := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
res_sign := Sign(encryData, now_str)
|
|
|
|
res.Data = proto.String(encryData)
|
|
res.Time = proto.String(now_str)
|
|
res.Sign = proto.String(res_sign)
|
|
}
|
|
|
|
}
|
|
|
|
func SetReqData(arg interface{}) (*Params, error) {
|
|
data_json, err := json.Marshal(arg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
now_int64 := time.Now().Unix()
|
|
|
|
encryData := crypter.DesEn(string(data_json), DES_KEY)
|
|
|
|
now := strconv.FormatInt(now_int64, 10)
|
|
|
|
sign := Sign(encryData, now)
|
|
|
|
return &Params{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil
|
|
|
|
}
|
|
|
|
func GetData(param *Params) (string, error) {
|
|
res_data := param.GetData()
|
|
|
|
if res_data != "" {
|
|
|
|
time_int64, err := strconv.ParseInt(param.GetTime(), 10, 64)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
now_int64 := time.Now().Unix()
|
|
|
|
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
|
|
//时间误差前后10秒,返回
|
|
return "", errors.New("返回时间错误")
|
|
}
|
|
|
|
check_sign := CheckSign(param.GetSign(), res_data, param.GetTime())
|
|
if !check_sign {
|
|
return "", errors.New("返回数据签名错误")
|
|
}
|
|
|
|
//解密
|
|
return crypter.DesDe(res_data, DES_KEY), nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
}
|