My implementation of AWS Request Authentication in Google Go lang
package main
import "fmt"
import "crypto/hmac"
import "crypto/sha256"
import "time"
import "encoding/base64"
func main() {
  AWSAccessKeyId := "MHAPUBLICKEY"
  AWSSecretKeyId := "MHAPRIVATEKEY"
  sha256         := sha256.New
  time           := time.Now().UTC().Format(time.ANSIC)
  hash           := hmac.New(sha256, []byte(AWSSecretKeyId))
  hash.Write([]byte(time))
  sha            := base64.URLEncoding.EncodeToString(hash.Sum(nil))
  fmt.Println("Date", time)
  fmt.Println("Content-Type","text/xml; charset=UTF-8")
  fmt.Println("AWS3-HTTPS AWSAccessKeyId=" + AWSAccessKeyId + ",Algorithm=HmacSHA256,Signature=" + sha)
}
I get valid output from Amazon but only when the 'sha' hash does not contain any _ or -
Working
'WFKzWNQlZEyTC9JFGFyqdf8AYj54aBj5btxPIaGTDbM='
Not Working HTTP/1.1 403 Forbidden SignatureDoesNotMatch
'h-FIs7of_CJ7LusAoQPzSWVt9hlXF_5gCQgedn_85lk='
How do I encode the AWS3-HTTPS header so it works in either circumstance? Just incase it's relevant, I am currently copy and pasting the output into cURL. I plan on implementing the request in Google Go once I have it working reliably.
I turned out I needed to change from URLEncoding to StdEncoding
sha = base64.URLEncoding.EncodeToString(hash.Sum(nil))
to
sha = base64.StdEncoding.EncodeToString(hash.Sum(nil))
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With