config.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package grace
  2. import (
  3. "crypto/sha256"
  4. "encoding/base64"
  5. "strings"
  6. "time"
  7. "github.com/google/uuid"
  8. )
  9. var APP_HOST = "http://api-v1-test.newtechlife.net"
  10. var APP_KEY = "ba8b372c5db54a36b589b1b4e5a4d2bf"
  11. var APP_SECRET = "39d30eac79be41ffb6e4d2434b9f6ca8"
  12. // 获取nonce
  13. func GetNonce() string {
  14. u, err := uuid.NewRandom()
  15. if err != nil {
  16. return ""
  17. }
  18. // 将 UUID 转换为字符串并去除破折号,得到 32 位字符
  19. return strings.Replace(u.String(), "-", "", -1)
  20. }
  21. // 获取created
  22. func GetCreated() string {
  23. now := time.Now()
  24. eightHoursAgo := now.Add(-8 * time.Hour)
  25. return eightHoursAgo.Format("2006-01-02T15:04:05Z")
  26. }
  27. // 获取password
  28. func Encrypt(nonce, created, app_secret string) string {
  29. data := nonce + created + app_secret
  30. utf8Data := []byte(strings.ToValidUTF8(data, ""))
  31. h := sha256.New()
  32. h.Write(utf8Data)
  33. hashBytes := h.Sum(nil)
  34. return base64.StdEncoding.EncodeToString(hashBytes)
  35. }
  36. func GetHeader() map[string]string {
  37. nonce := GetNonce()
  38. created := GetCreated()
  39. pwd := Encrypt(nonce, created, APP_SECRET)
  40. header := make(map[string]string)
  41. header["Accept"] = "application/json"
  42. header["Content-Type"] = "application/json"
  43. header["Authorization"] = "WSSE realm=\"SDP\", profile=\"UsernameToken\", type=\"Appkey\""
  44. header["X-WSSE"] = "UsernameToken Username=\"" + APP_KEY + "\", PasswordDigest=\"" + pwd + "\", Nonce=\"" + nonce + "\", Created=\"" + created + "\""
  45. return header
  46. }