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.
 
 
 

46 lines
1.3 KiB

  1. package ec2
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "github.com/goamz/goamz/aws"
  7. "sort"
  8. "strings"
  9. )
  10. // ----------------------------------------------------------------------------
  11. // EC2 signing (http://goo.gl/fQmAN)
  12. var b64 = base64.StdEncoding
  13. func sign(auth aws.Auth, method, path string, params map[string]string, host string) {
  14. params["AWSAccessKeyId"] = auth.AccessKey
  15. params["SignatureVersion"] = "2"
  16. params["SignatureMethod"] = "HmacSHA256"
  17. if auth.Token() != "" {
  18. params["SecurityToken"] = auth.Token()
  19. }
  20. // AWS specifies that the parameters in a signed request must
  21. // be provided in the natural order of the keys. This is distinct
  22. // from the natural order of the encoded value of key=value.
  23. // Percent and equals affect the sorting order.
  24. var keys, sarray []string
  25. for k, _ := range params {
  26. keys = append(keys, k)
  27. }
  28. sort.Strings(keys)
  29. for _, k := range keys {
  30. sarray = append(sarray, aws.Encode(k)+"="+aws.Encode(params[k]))
  31. }
  32. joined := strings.Join(sarray, "&")
  33. payload := method + "\n" + host + "\n" + path + "\n" + joined
  34. hash := hmac.New(sha256.New, []byte(auth.SecretKey))
  35. hash.Write([]byte(payload))
  36. signature := make([]byte, b64.EncodedLen(hash.Size()))
  37. b64.Encode(signature, hash.Sum(nil))
  38. params["Signature"] = string(signature)
  39. }