76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package dto
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
|
|
"sort"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
_ "github.com/beego/beego/v2/server/web"
|
|
"github.com/duke-git/lancet/v2/formatter"
|
|
)
|
|
|
|
func TestParams_Encrypt(t *testing.T) {
|
|
//orderParams := Params{
|
|
// GeneratedTime: time.Now().Unix(),
|
|
// Duration: 24,
|
|
// PayKey: "kkkkc9kit6bimggos5kk0100",
|
|
// OrderNo: "1234556",
|
|
// ProductCode: "6688",
|
|
// ShowMMValue: 100,
|
|
// NotifyUrl: "http://121.37.253.228:12307/",
|
|
//}
|
|
//p, _ := formatter.Pretty(orderParams)
|
|
//otel.Logger.WithContext(c.Ctx.Request.Context()).Info(p, "https://shop.kkknametrans.buzz/?sign="+orderParams.Encrypt())
|
|
|
|
main()
|
|
}
|
|
|
|
func main() {
|
|
//实际获取的appKey值
|
|
appKey := "1f2811dcd32b2c5c"
|
|
//实际获取的appSecret值
|
|
appSecret := "3C569A210D1260B2"
|
|
|
|
// 如果有其它参数,依次添加 key=value
|
|
params := map[string]string{}
|
|
|
|
params["appKey"] = appKey
|
|
params["phone"] = "13800138000"
|
|
params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
|
|
params["amount"] = "100"
|
|
|
|
result, _ := formatter.Pretty(params)
|
|
otelTrace.Logger.WithContext(otelTrace.InitCtx).Info(result)
|
|
|
|
strArr := SortMap(params)
|
|
signStr := ""
|
|
for i := 0; i < len(strArr); i++ {
|
|
k := strArr[i]
|
|
if len(params[k]) == 0 {
|
|
signStr += k
|
|
} else {
|
|
signStr += k + params[k]
|
|
}
|
|
}
|
|
signStr += appSecret
|
|
h := md5.New()
|
|
otelTrace.Logger.WithContext(otelTrace.InitCtx).Info(signStr)
|
|
h.Write([]byte(signStr))
|
|
result = hex.EncodeToString(h.Sum(nil))
|
|
otelTrace.Logger.WithContext(otelTrace.InitCtx).Info(result)
|
|
}
|
|
|
|
// SortMap 对map的key值进行排序
|
|
func SortMap(m map[string]string) []string {
|
|
var arr []string
|
|
for k := range m {
|
|
arr = append(arr, k)
|
|
}
|
|
sort.Strings(arr)
|
|
return arr
|
|
}
|