七牛的时间戳防盗链go语言

package main

import (
    "crypto/md5"
    "fmt"
    "net/url"
    "strings"
    "time"
)

func main() {
    urls :="http://pili-live-hdl.pilihu.echohu.top/huxicongPili/588773c868d03f791b030f4e.flv?123"
    urltimestamps:=&urltimestamp{
        urls,
        0,
        "123",

    }
    urlsign:=urltimestamps.timestamp()
    fmt.Println(urlsign)
    fmt.Println(time.Now().Unix())
}

type urltimestamp struct {
    urls string
    expecttime int64
    sign_key string
}

func (urlInfo *urltimestamp)timestamp()string  {
    data,err:=url.Parse(urlInfo.urls)
    if err !=nil{
        fmt.Println("er")


    }
    path:=data.Path
    splitData:=strings.Split(path,".")
    var sign_paths string
    if len(splitData)!=1 {

        sign_path:=splitData[0]
        sign_paths= sign_path[1:]
    }else {
        sign_paths=splitData[0]
    }
    query:=data.RawQuery

    expireTime :=fmt.Sprintf("%x", time.Now().Unix()+urlInfo.expecttime)

    var sign_token string
    if  query!=""{
        sign_token = fmt.Sprintf("%s%s%s%s", urlInfo.sign_key, expireTime ,sign_paths,query)
    }else {
        sign_token = fmt.Sprintf("%s%s%s", urlInfo.sign_key, sign_paths, expireTime)
    }
    fmt.Println(sign_token)
    sing:=md5_time(sign_token)
    var urlget string

    if  query!=""{
        rebuildPath:=fmt.Sprintf("%s://%s%s",data.Scheme,data.Host,data.Path)
        urlget=fmt.Sprintf(rebuildPath+"?s=%s&ts=%s&rid=%s",sing,expireTime,query)
    }else{
        urlget=fmt.Sprintf(urlInfo.urls+"?s=%s&ts=%s",sing,expireTime)

    }

    return urlget
}



func md5_time(str string) string {
    md5H := md5.New()
    md5H.Write([]byte(str))
    sign := fmt.Sprintf("%x", md5H.Sum(nil))
    return sign
}

你可能感兴趣的:(七牛)