7
0
gaore-common-sdk-go/sdk/auth/signers/access_key_siginer.go

45 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-08-04 00:51:19 +08:00
package signers
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
)
type AccessKeySigner struct {
credential *credentials.AccessKeyCredential
}
2020-08-06 10:35:24 +08:00
func (signer *AccessKeySigner) GetAccessKeyId() (string, error) {
return signer.credential.AccessKeyId, nil
2020-08-04 00:51:19 +08:00
}
2020-08-06 12:15:51 +08:00
func (signer *AccessKeySigner) GetAccessKeyFrom() (string, error) {
return signer.credential.AccessKeyFrom, nil
}
2020-08-04 00:51:19 +08:00
func (*AccessKeySigner) GetName() string {
return "HMAC-SHA1"
}
func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string {
secret := signer.credential.AccessKeySecret + secretSuffix
return ShaHmac1(stringToSign, secret)
}
2020-08-06 10:35:24 +08:00
func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner {
return &AccessKeySigner{
credential: credential,
}
}
2020-08-04 00:51:19 +08:00
func ShaHmac1(source, secret string) string {
key := []byte(secret)
hmac := hmac.New(sha1.New, key)
hmac.Write([]byte(source))
signedBytes := hmac.Sum(nil)
signedString := base64.StdEncoding.EncodeToString(signedBytes)
return signedString
}