Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Request Authentication: Encode Header

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.

like image 301
Karl Entwistle Avatar asked Oct 04 '22 09:10

Karl Entwistle


1 Answers

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))
like image 181
Karl Entwistle Avatar answered Oct 13 '22 09:10

Karl Entwistle