|
|
- package sms
-
- import (
- "bytes"
- "crypto/md5"
- "encoding/hex"
- "git.tetele.net/tgo/helper"
- "io/ioutil"
- "log"
- "net/http"
- "net/url"
- "strings"
-
- "github.com/json-iterator/go"
- "github.com/tjfoc/gmsm/sm3"
- )
-
- var json = jsoniter.ConfigCompatibleWithStandardLibrary
-
- // 密码加密
- func Md5Str(str ...string) string {
-
- var build strings.Builder
- if len(str) > 0 {
- for _, v := range str {
- build.WriteString(v)
- }
- } else {
- return ""
- }
-
- h := md5.New()
- h.Write([]byte(build.String())) // 需要加密的字符串
- cipher2Str := h.Sum(nil)
- sMd5 := hex.EncodeToString(cipher2Str) // 输出加密结果
- return sMd5
- }
-
- /*
- * 连接多个字符串
- */
- func StringJoin(s ...string) string {
- var build strings.Builder
- if len(s) > 0 {
- for _, v := range s {
- build.WriteString(v)
- }
- }
-
- return build.String()
- }
-
- /**
- * post 请求
- */
- func PostJsonData(url string, param interface{}, header ...map[string]string) ([]byte, error) {
- httpClient := &http.Client{}
- params, err := json.Marshal(param)
- if err != nil {
- return []byte(""), err
- }
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(params))
- if err != nil {
- return []byte(""), err
- }
-
- req.Header.Set("Content-Type", "application/json")
-
- if len(header) > 0 {
- for _, item := range header {
- for k, v := range item {
- req.Header[k] = []string{v}
- }
- }
- }
- resp, err := httpClient.Do(req)
- if err != nil {
- return []byte(""), err
- }
-
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return []byte(""), err
- }
-
- return body, nil
- }
-
- // ums验签
- func UmsSetSign(data, header map[string]string,secretKey string) string {
- str := helper.HttpBuildQuery(data)
- // url encode
- str = url.QueryEscape(str)
- str = header["x-sp-code"] + header["x-app-key"] + secretKey + str + header["x-request-id"]
- log.Println("加密前数据: ", str)
- // ms3加密
- hash := sm3.New()
- hash.Write([]byte(str))
- sign_byte := hash.Sum(nil)
- sign := hex.EncodeToString(sign_byte)
- log.Println("加密后数据: ", sign)
- return sign
- }
|