github.com/qiniu/go-sdk/v7@v7.27.0/cdn/anti_leech.go (about) 1 package cdn 2 3 import ( 4 "crypto/md5" 5 "fmt" 6 "net/url" 7 "time" 8 ) 9 10 // CreateTimestampAntileechURL 用来构建七牛CDN时间戳防盗链的访问链接 11 func CreateTimestampAntileechURL(urlStr string, encryptKey string, 12 durationInSeconds int64, 13 ) (antileechURL string, err error) { 14 u, err := url.Parse(urlStr) 15 if err != nil { 16 return 17 } 18 19 expireTime := time.Now().Add(time.Second * time.Duration(durationInSeconds)).Unix() 20 toSignStr := fmt.Sprintf("%s%s%x", encryptKey, u.EscapedPath(), expireTime) 21 signedStr := fmt.Sprintf("%x", md5.Sum([]byte(toSignStr))) 22 23 q := url.Values{} 24 q.Add("sign", signedStr) 25 q.Add("t", fmt.Sprintf("%x", expireTime)) 26 27 if u.RawQuery == "" { 28 antileechURL = u.String() + "?" + q.Encode() 29 } else { 30 antileechURL = u.String() + "&" + q.Encode() 31 } 32 33 return 34 }