5

我在 Google Go 语言中实现 AWS 请求身份验证

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

我从 Amazon 获得有效输出,但仅当“sha”哈希不包含任何 _ 或 -

在职的

'WFKzWNQlZEyTC9JFGFyqdf8AYj54aBj5btxPIaGTDbM='

不工作 HTTP/1.1 403 Forbidden SignatureDoesNotMatch

'h-FIs7of_CJ7LusAoQPzSWVt9hlXF_5gCQgedn_85lk='

如何对 AWS3-HTTPS 标头进行编码,使其在任何一种情况下都能正常工作?以防万一它是相关的,我目前正在将输出复制并粘贴到 cURL 中。一旦我让它可靠地工作,我计划在 Google Go 中实施该请求。

4

1 回答 1

4

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))
于 2013-04-22T21:40:10.933 回答