You need to enable JavaScript to run this app.
导航
鉴权示例
最近更新时间:2024.07.12 16:43:26首次发布时间:2022.08.24 18:12:16

本文档将为您介绍 veImageX 域名配置中鉴权方式A鉴权方式B鉴权方式C鉴权方式D的实现方法,以下为具体的 Demo 示例。

func getMD5(s string) string {
   h := md5.New()
   h.Write([]byte(s))
   return hex.EncodeToString(h.Sum(nil))
}

// 鉴权方式A
func authA(domain, path, secretKey, signParam string) string {
   u := url.URL{
      Scheme: "http", // 支持 http、https
      Host:   domain,
      Path:   path,
   }
   ts := time.Now().Unix()
   rand := "rand"  // 随机字符串,默认值为 "rand"。
   uid := "imagex" // 用户 ID,默认值为 "imagex"。

   //path + timestamp + rand + uid + key
   signTxt := fmt.Sprintf("%s-%d-%s-%s-%s", u.EscapedPath(), ts, rand, uid, secretKey)
   sign := fmt.Sprintf("%d-%s-%s-%s", ts, rand, uid, getMD5(signTxt))
   q := url.Values{
      signParam: []string{sign},
   }
   u.RawQuery = q.Encode()
   return u.String()
}

// 鉴权方式B
func authB(domain, path, secretKey string) string {
   u := url.URL{
      Scheme: "http", // 支持 http、https
      Host:   domain,
      Path:   path,
   }
   tn := time.Now()
   tStr := tn.Format("200601021504")

   //key + tStr + path
   signTxt := fmt.Sprintf("%s%s%s", secretKey, tStr, u.EscapedPath())
   sign := getMD5(signTxt)
   u.Path = fmt.Sprintf("/%s/%s%s", tStr, sign, path)
   return u.String()
}

// 鉴权方式C
func authC(domain, path, secretKey string) string {
   u := url.URL{
      Scheme: "http", // 支持 http、https
      Host:   domain,
      Path:   path,
   }
   ts := time.Now().Unix()

   //key + path + hex(ts)
   signTxt := fmt.Sprintf("%s%s%x", secretKey, u.EscapedPath(), ts)
   sign := getMD5(signTxt)
   u.Path = fmt.Sprintf("/%s/%x%s", sign, ts, path)
   return u.String()
}

// 鉴权方式D
func authD(domain, path, secretKey, signParam, timestampFormat, timestampParam string) string {
   u := url.URL{
      Scheme: "http", // 支持 http、https
      Host:   domain,
      Path:   path,
   }
   ts := time.Now().Unix()
   tsStr := fmt.Sprintf("%d", ts)
   if timestampFormat == "hex" {
      tsStr = fmt.Sprintf("%x", ts)
   }

   //key + path + ts
   signTxt := fmt.Sprintf("%s%s%s", secretKey, u.EscapedPath(), tsStr)
   sign := getMD5(signTxt)
   q := url.Values{
      signParam:      []string{sign},
      timestampParam: []string{tsStr},
   }
   u.RawQuery = q.Encode()
   return u.String()
}

func main() {
   domain := "example.volcimagex.cn"
   uri := "<input resource uri>"
   tpl := "input resource template"
   format := "input output image format"

   // 图片资源
   path := fmt.Sprintf("/%s~%s.%s", uri, tpl, format)
   // 素材托管
   //path = fmt.Sprintf("/%s", uri)

   signParam := "<input sign parameter key>"
   mainSecretKey := "<input secret key>"
   u := authA(domain, path, mainSecretKey, signParam)
   fmt.Println("URL: ", u)

   u = authB(domain, path, mainSecretKey)
   fmt.Println("URL: ", u)

   u = authC(domain, path, mainSecretKey)
   fmt.Println("URL: ", u)

   timeParam := "<input timestamp parameter key>"
   //十进制 or 十六进制
   timeFormat := "<input timestamp format>"
   u = authD(domain, path, mainSecretKey, signParam, timeFormat, timeParam)
   fmt.Println("URL: ", u)
}