package grace import ( "crypto/sha256" "encoding/base64" "strings" "time" "github.com/google/uuid" ) var APP_HOST = "http://api-v1-test.newtechlife.net" var APP_KEY = "ba8b372c5db54a36b589b1b4e5a4d2bf" var APP_SECRET = "39d30eac79be41ffb6e4d2434b9f6ca8" // 获取nonce func GetNonce() string { u, err := uuid.NewRandom() if err != nil { return "" } // 将 UUID 转换为字符串并去除破折号,得到 32 位字符 return strings.Replace(u.String(), "-", "", -1) } // 获取created func GetCreated() string { now := time.Now() eightHoursAgo := now.Add(-8 * time.Hour) return eightHoursAgo.Format("2006-01-02T15:04:05Z") } // 获取password func Encrypt(nonce, created, app_secret string) string { data := nonce + created + app_secret utf8Data := []byte(strings.ToValidUTF8(data, "")) h := sha256.New() h.Write(utf8Data) hashBytes := h.Sum(nil) return base64.StdEncoding.EncodeToString(hashBytes) } func GetHeader() map[string]string { nonce := GetNonce() created := GetCreated() pwd := Encrypt(nonce, created, APP_SECRET) header := make(map[string]string) header["Accept"] = "application/json" header["Content-Type"] = "application/json" header["Authorization"] = "WSSE realm=\"SDP\", profile=\"UsernameToken\", type=\"Appkey\"" header["X-WSSE"] = "UsernameToken Username=\"" + APP_KEY + "\", PasswordDigest=\"" + pwd + "\", Nonce=\"" + nonce + "\", Created=\"" + created + "\"" return header }