|
@@ -0,0 +1,77 @@
|
|
|
+package analysis
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "crypto/hmac"
|
|
|
+ "crypto/sha1"
|
|
|
+ "encoding/base64"
|
|
|
+ "hash"
|
|
|
+ "io"
|
|
|
+ "log"
|
|
|
+ "net/url"
|
|
|
+ "sort"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+type SignStr struct {
|
|
|
+ Key []string
|
|
|
+ Val []string
|
|
|
+}
|
|
|
+
|
|
|
+var secret = "20202020"
|
|
|
+
|
|
|
+func Signature(param [][]string) (signedStr string) {
|
|
|
+ sgt := &SignStr{[]string{}, []string{}}
|
|
|
+ for k, v := range param {
|
|
|
+ log.Println(k, v)
|
|
|
+ sgt.Key = append(sgt.Key, v[0])
|
|
|
+ sgt.Val = append(sgt.Val, v[1])
|
|
|
+ }
|
|
|
+ sgt.Sort()
|
|
|
+ reqStr := sgt.ToString()
|
|
|
+ str := percentEncode(reqStr)
|
|
|
+ log.Println("str", str)
|
|
|
+ h := hmac.New(func() hash.Hash {
|
|
|
+ return sha1.New()
|
|
|
+ }, []byte(secret))
|
|
|
+ io.WriteString(h, str)
|
|
|
+ signedStr = base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
+ log.Println(signedStr)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+var Sp = strings.Replace
|
|
|
+
|
|
|
+func percentEncode(str string) string {
|
|
|
+ str = url.QueryEscape(str)
|
|
|
+ str = Sp(Sp(Sp(str, "+", "%20", -1), "*", "%2A", -1), "&7E", "~", -1)
|
|
|
+ return str
|
|
|
+}
|
|
|
+
|
|
|
+func (this *SignStr) ToString() string {
|
|
|
+ str := ""
|
|
|
+ for k, v := range this.Key {
|
|
|
+ if k > 0 {
|
|
|
+ str += "&"
|
|
|
+ }
|
|
|
+ str += v + "=" + this.Val[k]
|
|
|
+ }
|
|
|
+ return str
|
|
|
+}
|
|
|
+
|
|
|
+func (this *SignStr) Sort() {
|
|
|
+ sort.Sort(this)
|
|
|
+}
|
|
|
+
|
|
|
+func (this *SignStr) Len() int {
|
|
|
+ return len(this.Val)
|
|
|
+}
|
|
|
+
|
|
|
+func (this *SignStr) Less(i, j int) bool {
|
|
|
+ return bytes.Compare([]byte(this.Key[i]), []byte(this.Key[j])) < 1
|
|
|
+}
|
|
|
+
|
|
|
+func (this *SignStr) Swap(i, j int) {
|
|
|
+ this.Key[i], this.Key[j] = this.Key[j], this.Key[i]
|
|
|
+ this.Val[i], this.Val[j] = this.Val[j], this.Val[i]
|
|
|
+}
|