45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package signers
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"golib.gaore.com/GaoreGo/haiwai-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
|
|
}
|