package main
import (
"crypto/md5"
"flag"
"fmt"
"net/url"
"strings"
)
func main() {
var t int64
var key string
var resUrl string
flag.Int64Var(&t, "t", 0, "expire timestamp")
flag.StringVar(&key, "key", "", "encrypt key")
flag.StringVar(&resUrl, "url", "", "resource url")
flag.Parse()
if t == 0 || key == "" || resUrl == "" {
return
}
expireTime := fmt.Sprintf("%x", t)
resUri, pErr := url.Parse(resUrl)
if pErr != nil {
return
}
path := resUri.Path
rawStr := fmt.Sprintf("%s%s%s", key, path, expireTime)
md5H := md5.New()
md5H.Write([]byte(rawStr))
sign := fmt.Sprintf("%x", md5H.Sum(nil))
var newUrl string
if strings.Contains(resUrl, "?") {
newUrl = fmt.Sprintf("%s&sign=%s&t=%s", resUrl, sign, expireTime)
} else {
newUrl = fmt.Sprintf("%s?sign=%s&t=%s", resUrl, sign, expireTime)
}
fmt.Println(newUrl)
}
如何作到防盗?i没太理解。请指教,谢谢
#1