You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

  1. package signers
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha1"
  5. "encoding/base64"
  6. "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
  7. )
  8. type AccessKeySigner struct {
  9. credential *credentials.AccessKeyCredential
  10. }
  11. func (signer *AccessKeySigner) GetAccessKeyId() (string, error) {
  12. return signer.credential.AccessKeyId, nil
  13. }
  14. func (signer *AccessKeySigner) GetAccessKeyFrom() (string, error) {
  15. return signer.credential.AccessKeyFrom, nil
  16. }
  17. func (*AccessKeySigner) GetName() string {
  18. return "HMAC-SHA1"
  19. }
  20. func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string {
  21. secret := signer.credential.AccessKeySecret + secretSuffix
  22. return ShaHmac1(stringToSign, secret)
  23. }
  24. func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner {
  25. return &AccessKeySigner{
  26. credential: credential,
  27. }
  28. }
  29. func ShaHmac1(source, secret string) string {
  30. key := []byte(secret)
  31. hmac := hmac.New(sha1.New, key)
  32. hmac.Write([]byte(source))
  33. signedBytes := hmac.Sum(nil)
  34. signedString := base64.StdEncoding.EncodeToString(signedBytes)
  35. return signedString
  36. }