消息服务RPC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.3 KiB

3 years ago
  1. package msgrpc
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "errors"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "git.tetele.net/tgo/crypter"
  10. )
  11. /**
  12. * 签名
  13. */
  14. func Sign(data string, salt string) string {
  15. var build strings.Builder
  16. build.WriteString(data)
  17. build.WriteString(salt)
  18. build.WriteString(DES_KEY)
  19. data_str := build.String()
  20. h := md5.New()
  21. h.Write([]byte(data_str)) // 需要加密的字符串
  22. return hex.EncodeToString(h.Sum(nil)) // 输出加密结果
  23. }
  24. /**
  25. * 验证签名
  26. */
  27. func CheckSign(sign_str, data, salt string) bool {
  28. sign := Sign(data, salt)
  29. if strings.Compare(sign_str, sign) > -1 {
  30. return true
  31. }
  32. return false
  33. }
  34. /**
  35. * 解密
  36. */
  37. func GetOrgData(res *Response) (string, error) {
  38. res_data := res.GetData()
  39. if res_data == "" {
  40. return "", errors.New("未收到收据")
  41. }
  42. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  43. if err != nil {
  44. return "", err
  45. }
  46. now_int64 := time.Now().Unix()
  47. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  48. //时间误差前后10秒,返回
  49. return "", errors.New("返回时间错误")
  50. }
  51. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  52. if !check_sign {
  53. return "", errors.New("返回数据签名错误")
  54. }
  55. //解密
  56. res_data_de := crypter.DesDe(res_data, DES_KEY)
  57. return res_data_de, nil
  58. }