7
0
gaore-common-sdk-go/sdk/auth/signers/ali_appcode_signer.go
liangzy 69c6020214 1.增加roa请求,
2.增加阿里(rali) 验签请求加密方式
2020-09-23 12:14:23 +08:00

44 lines
1.0 KiB
Go

package signers
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
)
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
}