haiwai-common-sdk-go/sdk/auth/signers/sts_token_signer.go

42 lines
967 B
Go
Raw Permalink Normal View History

2023-02-21 20:46:05 +08:00
package signers
import (
"crypto/md5"
"fmt"
2023-02-21 20:58:53 +08:00
"golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk/auth/credentials"
2023-02-21 20:46:05 +08:00
)
type StsTokenSigner struct {
credential *credentials.StdTokenCredential
}
func (signer *StsTokenSigner) GetAccessKeyId() (string, error) {
return signer.credential.AccessKeyId, nil
}
func (signer *StsTokenSigner) GetAccessKeyFrom() (string, error) {
return signer.credential.AccessKeyFrom, nil
}
func (*StsTokenSigner) GetName() string {
return "MD5"
}
func (signer *StsTokenSigner) Sign(stringToSign, secretSuffix string) string {
secret := signer.credential.AccessKeySecret + secretSuffix
return Md5(stringToSign, secret)
}
func NewStsTokenSigner(credential *credentials.StdTokenCredential) *StsTokenSigner {
return &StsTokenSigner{
credential: credential,
}
}
func Md5(source, secret string) string {
data := []byte(fmt.Sprintf("%s##%s", secret, source))
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has)
return md5str
}