You need to enable JavaScript to run this app.
导航
鉴权示例
最近更新时间:2023.03.16 17:01:30首次发布时间: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 {
   ts := time.Now().Unix()
   rand := "rand"  // "rand" by default, any value you defined is ok
   uid := "imagex" // "imagex" by default, any value you defined is ok

   //path + timestamp + rand + uid + key
   signTxt := fmt.Sprintf("%s-%d-%s-%s-%s", path, ts, rand, uid, secretKey)
   sign := getMD5(signTxt)
   signValue := fmt.Sprintf("%d-%s-%s-%s", ts, rand, uid, sign)
   return fmt.Sprintf("http://%s%s?%s=%s", domain, path, signParam, signValue)
}

// 鉴权方式B
func authB(domain, path, secretKey string) string {
   tn := time.Now()
   tStr := tn.Format("200601021504")

   //key + tStr + path
   signTxt := fmt.Sprintf("%s%s%s", secretKey, tStr, path)
   sign := getMD5(signTxt)
   signPath := fmt.Sprintf("/%s/%s", tStr, sign)
   return fmt.Sprintf("http://%s%s%s", domain, signPath, path)
}

// 鉴权方式C
func authC(domain, path, secretKey string) string {
   ts := time.Now().Unix()

   //key + path + hex(ts)
   signTxt := fmt.Sprintf("%s%s%x", secretKey, path, ts)
   sign := getMD5(signTxt)
   signPath := fmt.Sprintf("/%s/%x", sign, ts)
   return fmt.Sprintf("http://%s%s%s", domain, signPath, path)
}

// 鉴权方式D
func authD(domain, path, secretKey, signParam, timestampFormat, timestampParam string) string {
   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, path, tsStr)
   sign := getMD5(signTxt)
   qs := url.Values{}
   qs.Add(signParam, sign)
   qs.Add(timestampParam, tsStr)

   return fmt.Sprintf("http://%s%s?%s", domain, path, qs.Encode())
}

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)
}