其他分享
首页 > 其他分享> > golang map做排序后,md5加密

golang map做排序后,md5加密

作者:互联网

Go map字典排序后md5加密

 

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"

    // 排序
    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)))
}

 

标签:map,keys,fmt,golang,params,byteBuf,md5
来源: https://www.cnblogs.com/zhaoyingjie/p/15826190.html