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

时间:2013-04-16 21:32:45

标签: amazon-web-services go amazon-route53

我在Google Go lang中实施AWS Request身份验证

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)
}

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

工作

  

'WFKzWNQlZEyTC9JFGFyqdf8AYj54aBj5btxPIaGTDbM ='

不工作HTTP / 1.1 403 Forbidden SignatureDoesNotMatch

  

'h-FIs7of_CJ7LusAoQPzSWVt9hlXF_5gCQgedn_85lk ='

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

1 个答案:

答案 0 :(得分:4)

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

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

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