英文:
AWS Request Authentication: Encode Header
问题
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)
}
英文:
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.
答案1
得分: 4
我发现我需要从URLEncoding更改为StdEncoding
sha = base64.StdEncoding.EncodeToString(hash.Sum(nil))
到
sha = base64.StdEncoding.EncodeToString(hash.Sum(nil))
英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论