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

45 lines
1.1 KiB
Go
Raw Normal View History

2023-02-21 20:46:05 +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
}
func (signer *AccessKeySigner) GetAccessKeyId() (string, error) {
return signer.credential.AccessKeyId, nil
}
func (signer *AccessKeySigner) GetAccessKeyFrom() (string, error) {
return signer.credential.AccessKeyFrom, nil
}
func (*AccessKeySigner) GetName() string {
return "HMAC-SHA1"
}
func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string {
secret := signer.credential.AccessKeySecret + secretSuffix
return ShaHmac1(stringToSign, secret)
}
func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner {
return &AccessKeySigner{
credential: credential,
}
}
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
}