AWS请求身份验证:编码标头

Kar*_*tle 5 go amazon-web-services amazon-route53

我在Google Go lang中实施AWS Request Authentication

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)
}
Run Code Online (Sandbox Code Playgroud)

我从亚马逊获得有效输出,但只有当'sha'哈希不包含任何_或 - 时

工作

'WFKzWNQlZEyTC9JFGFyqdf8AYj54aBj5btxPIaGTDbM ='

不工作HTTP/1.1 403 Forbidden SignatureDoesNotMatch

'H-FIs7of_CJ7LusAoQPzSWVt9hlXF_5gCQgedn_85lk ='

如何对AWS3-HTTPS标头进行编码,使其适用于任何一种情况?只是因为它是相关的,我目前正在复制并将输出粘贴到cURL中.我计划在Google Go中实现请求,并使其可靠地运行.

Kar*_*tle 4

我发现我需要从 URLEncoding 更改为 StdEncoding

sha = base64.URLEncoding.EncodeToString(hash.Sum(nil))
Run Code Online (Sandbox Code Playgroud)

sha = base64.StdEncoding.EncodeToString(hash.Sum(nil))
Run Code Online (Sandbox Code Playgroud)