44 lines
1.0 KiB
Go
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
|
||
|
}
|