From 17a056688794e1943d2fca97f7e19b217d35df59 Mon Sep 17 00:00:00 2001 From: guzeng Date: Sat, 4 Sep 2021 16:19:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=AC=E5=85=B1=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 data.go diff --git a/data.go b/data.go new file mode 100644 index 0000000..cecf768 --- /dev/null +++ b/data.go @@ -0,0 +1,106 @@ +package siterpc + +import ( + "encoding/json" + "errors" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + "github.com/golang/protobuf/proto" +) + +func SetResData(data interface{}, res *Response) { + 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{}) (*Request, 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 &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil + +} + +func GetReqData(req *Request) (string, error) { + res_data := req.GetData() + + if res_data != "" { + + time_int64, err := strconv.ParseInt(req.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(req.GetSign(), res_data, req.GetTime()) + if !check_sign { + return "", errors.New("返回数据签名错误") + } + + //解密 + return crypter.DesDe(res_data, DES_KEY), nil + + } + + return "", nil +} + +func GetResData(res *Response) (string, error) { + res_data := res.GetData() + + if res_data != "" { + + time_int64, err := strconv.ParseInt(res.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(res.GetSign(), res_data, res.GetTime()) + if !check_sign { + return "", errors.New("返回数据签名错误") + } + + //解密 + return crypter.DesDe(res_data, DES_KEY), nil + + } + + return "", nil +}