AWS Request Authentication: Header Encoding

My implementation of AWS request authentication in go 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 -

AT

'WFKzWNQlZEyTC9JFGFyqdf8AYj54aBj5btxPIaGTDbM ='

HTTP / 1.1 does not work 403 Forbidden Signature

'h-FIs7of_CJ7LusAoQPzSWVt9hlXF_5gCQgedn_85lk ='

How to encode the AWS3-HTTPS header so that it works anyway? Just make it relevant, I am currently copying and pasting the output into cURL. I plan to fulfill the request on Google Go if I have reliable work.

+5
source share
1 answer

It seemed to me that I needed to switch from URLEncoding to StdEncoding

sha = base64.URLEncoding.EncodeToString(hash.Sum(nil))

to

sha = base64.StdEncoding.EncodeToString(hash.Sum(nil))
+4
source

All Articles