github.com/jtzjtz/kit@v1.0.2/sign/sn.go (about) 1 package sign 2 3 import ( 4 "fmt" 5 "net/url" 6 "sort" 7 "strings" 8 "time" 9 ) 10 11 func CreateSn(data map[string]interface{}, secret string, isDate bool) string { 12 var keys []string 13 var isFirst = true 14 var str = "" 15 for k, _ := range data { 16 if k != "token" { 17 keys = append(keys, k) 18 } 19 } 20 sort.Strings(keys) 21 for _, v := range keys { 22 if isFirst { 23 str = fmt.Sprintf("%v=%v", v, data[v]) 24 isFirst = false 25 } else { 26 str = str + fmt.Sprintf("&%v=%v", v, data[v]) 27 } 28 } 29 str = str + secret 30 if isDate { 31 str = str + time.Now().Format("2006-01-02") 32 } 33 token := strings.ToLower(ToMd5(str)) 34 return token 35 } 36 37 func CheckSn(data url.Values, sn string, secret string, isDate bool) bool { 38 if sn == "" { 39 return false 40 } 41 if strings.Compare(sn, CreateSnByFormData(data, secret, isDate)) == 0 { 42 return true 43 } 44 return false 45 } 46 47 func CreateSnByFormData(data url.Values, secret string, isDate bool) string { 48 var keys []string 49 var isFirst = true 50 var str = "" 51 52 for k, _ := range data { 53 if k != "token" { 54 keys = append(keys, k) 55 56 } 57 58 } 59 sort.Strings(keys) 60 61 for _, v := range keys { 62 if isFirst { 63 64 str = fmt.Sprintf("%v=%v", v, data.Get(v)) 65 isFirst = false 66 } else { 67 str = str + fmt.Sprintf("&%v=%v", v, data.Get(v)) 68 69 } 70 } 71 str = str + secret 72 if isDate { 73 str = str + time.Now().Format("2006-01-02") 74 } 75 76 return strings.ToLower(ToMd5(str)) 77 }