/* * @Descripttion: * @version: * @Author: Neo,Huang * @Date: 2020-10-16 14:29:56 * @LastEditors: Neo,Huang * @LastEditTime: 2020-12-16 14:27:06 */ package utils import ( "crypto/md5" "fmt" "io" "net/url" "sort" "github.com/astaxie/beego" "strconv" "encoding/hex" ) // Kv struct type Kv struct { Key, Value string } // Kvs typenmae type Kvs []*Kv func (a Kvs) Len() int { return len(a) } func (a Kvs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a Kvs) Less(i, j int) bool { return a[i].Key < a[j].Key } var secrets map[string]string func init_secrets() { secrets = make(map[string]string) secrets["101"] = "39baf5e79adac446131b7c78608cfe3d" secrets["102"] = "51f4e76fc3b42c56243e9aa504ddfc9e" } // 获取appkey对应的appSecret func GetSecretByKey(appKey string) string { if appKey == "" { return "" } secret, ok := secrets[appKey] if ok == false { init_secrets() secret, ok = secrets[appKey] } return secret } //Md5 md5散列函数 func Md5(str string) string { h := md5.New() io.WriteString(h, str) return fmt.Sprintf("%x", h.Sum(nil)) } var keyMahjong = "am0xNjAzOTUxODE2" // MakeSign 生成签名秘钥 func MakeSign(m map[string]string, appKey string) string { var kvs Kvs for k, v := range m { kvs = append(kvs, &Kv{k, url.QueryEscape(v)}) } sort.Sort(kvs) key := "" for i := 0; i < kvs.Len(); i++ { key += kvs[i].Key + kvs[i].Value } if appKey != "" { key += appKey } return Md5(key) } // MakeSign 生成签名秘钥 func MakeKeySign(m map[string]interface{}) string { var kvs Kvs for k, v := range m { switch val := v.(type) { case int: kvs = append(kvs, &Kv{k, IntToString(val)}) case int64: kvs = append(kvs, &Kv{k, strconv.FormatInt(val, 10)}) case string: kvs = append(kvs, &Kv{k, url.QueryEscape(val)}) } } sort.Sort(kvs) key := "" for i := 0; i < kvs.Len(); i++ { key += kvs[i].Key + kvs[i].Value } if beego.AppConfig.String("project") == "mj" { key += keyMahjong } return Md5(key) } // Strtomd5 md5 func Strtomd5(s string) string { h := md5.New() h.Write([]byte(s)) rs := hex.EncodeToString(h.Sum(nil)) return rs }