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

44 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-02-21 20:46:05 +08:00
package signers
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
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 AliAppcodeSigner struct {
credential *credentials.AliAppcodeCredential
}
func (signer *AliAppcodeSigner) GetName() string {
return "HmacSHA256"
}
func (signer *AliAppcodeSigner) GetAccessKeyId() (string, error) {
return signer.credential.AccessKeyId, nil
}
func (signer *AliAppcodeSigner) GetAccessKeyFrom() (string, error) {
return "", nil
}
func (signer *AliAppcodeSigner) Sign(stringToSign, secretSuffix string) string {
secret := signer.credential.AccessKeySecret + secretSuffix
return ShaHmac256(stringToSign, secret)
}
func NewAliAppcodeSigner(credential *credentials.AliAppcodeCredential) *AliAppcodeSigner {
return &AliAppcodeSigner{
credential: credential,
}
}
func ShaHmac256(source, secret string) string {
key := []byte(secret)
hmac1 := hmac.New(sha256.New, key)
hmac1.Write([]byte(source))
signedString := base64.StdEncoding.EncodeToString(hmac1.Sum(nil))
return signedString
}