forked from TrueCloudLab/distribution
90bed67126
Change-Id: I423ad03e63bd38aded3abfcba49079ff2fbb3b74 Signed-off-by: Li Yi <denverdino@gmail.com>
39 lines
1 KiB
Go
39 lines
1 KiB
Go
package util
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
//CreateSignature creates signature for string following Aliyun rules
|
|
func CreateSignature(stringToSignature, accessKeySecret string) string {
|
|
// Crypto by HMAC-SHA1
|
|
hmacSha1 := hmac.New(sha1.New, []byte(accessKeySecret))
|
|
hmacSha1.Write([]byte(stringToSignature))
|
|
sign := hmacSha1.Sum(nil)
|
|
|
|
// Encode to Base64
|
|
base64Sign := base64.StdEncoding.EncodeToString(sign)
|
|
|
|
return base64Sign
|
|
}
|
|
|
|
func percentReplace(str string) string {
|
|
str = strings.Replace(str, "+", "%20", -1)
|
|
str = strings.Replace(str, "*", "%2A", -1)
|
|
str = strings.Replace(str, "%7E", "~", -1)
|
|
|
|
return str
|
|
}
|
|
|
|
// CreateSignatureForRequest creates signature for query string values
|
|
func CreateSignatureForRequest(method string, values *url.Values, accessKeySecret string) string {
|
|
|
|
canonicalizedQueryString := percentReplace(values.Encode())
|
|
|
|
stringToSign := method + "&%2F&" + url.QueryEscape(canonicalizedQueryString)
|
|
return CreateSignature(stringToSign, accessKeySecret)
|
|
}
|