Example #1
0
func (auth BaiduAuth) Sign(signParam *SignParameter) (sUrl string, err error) {

	var u bytes.Buffer

	s, err := auth.sign(signParam)

	u.WriteString("http://")

	if auth.Host() == "" {
		err = errors.New("Host is empty")
		return
	}

	u.WriteString(strings.Trim(auth.Host(), "/"))
	u.WriteString("/")

	u.WriteString(strings.Trim(signParam.Bucket, "/"))

	objStr := strings.TrimLeft(signParam.Object, "/")

	if len(objStr) > 0 {
		u.WriteString("/")
		u.WriteString(url.QueryEscape(utils.UrlPreEncode(objStr)))
	}

	u.WriteString("?sign=")
	u.WriteString(s)
	if signParam.Time != "" {
		u.WriteString("&time=")
		u.WriteString(signParam.Time)
	}

	if signParam.Ip != "" {
		u.WriteString("&ip=")
		u.WriteString(signParam.Ip)
	}

	if signParam.Size != "" {
		u.WriteString("&size=")
		u.WriteString(signParam.Size)
	}

	sUrl = u.String()
	return
}
Example #2
0
func (auth BaiduAuth) sign(signParam *SignParameter) (result string, err error) {
	flag, data := content(signParam)

	var f func() hash.Hash = sha1.New

	h := hmac.New(f, auth.Secret())
	h.Write(data)
	signed := base64.URLEncoding.EncodeToString(h.Sum(nil))

	var buff bytes.Buffer
	buff.WriteString(flag)
	buff.WriteString(":")
	buff.WriteString(auth.Key())
	buff.WriteString(":")
	buff.WriteString(url.QueryEscape(utils.UrlPreEncode(signed)))
	result = buff.String()
	return
}