After golang map is sorted, md5 encryption

golang map lexicographically sorted with md5 encryption

package main
import (
     "bytes"
     "crypto/md5"
     "encoding/hex"
     "encoding/json"
     "fmt"
     "sort"
     "strings"
)
func main() {
     params := make(map[string]interface{})
     params["name"] = "test"
     params["key"] = "123aa!31."
     params["domain"] = "https://www.baidu.com?name=1&id=1"
     // sort
     keys := make([]string, len(params))
     i := 0
     for k, _ := range params {
         keys[i] = k
         i++
     }
     sort.Strings(keys)
     fmt.Println("keys:",keys)
     byteBuf := bytes.NewBuffer([]byte{})
     encoder := json.NewEncoder(byteBuf)
     encoder.SetEscapeHTML(false)
     err := encoder.Encode(params)
     if err != nil {
         panic(err)
     }
     data := byteBuf.String()
     h := md5.New()
     h.Write([]byte(strings.TrimRight(data, "\n")))
     fmt.Println(hex.EncodeToString(h.Sum(nil)))
}

Leave a Reply

Your email address will not be published.

en_USEnglish