From 41b1c701833334a3da736d21131a9e979366cd1d Mon Sep 17 00:00:00 2001 From: Joe Paul Date: Fri, 7 Dec 2018 11:57:08 +0530 Subject: [PATCH] ref: DRY hmac method (#1) --- policy.go | 2 +- sign.go | 35 ++++++++++++++--------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/policy.go b/policy.go index c70bd8d..4d62bb7 100644 --- a/policy.go +++ b/policy.go @@ -59,7 +59,7 @@ var NowTime = func() time.Time { return time.Now() } -var lf = []byte{'\n'} +var newLine = []byte{'\n'} // CreateUploadPolicies creates amazon s3 sigv4 compatible // policy and signing keys with the signature returns the upload policy. diff --git a/sign.go b/sign.go index 4d8b57e..e5faf34 100644 --- a/sign.go +++ b/sign.go @@ -7,7 +7,6 @@ package simples3 import ( "bytes" - "crypto/hmac" "crypto/sha256" "fmt" "io" @@ -21,10 +20,10 @@ import ( ) func (s3 *S3) signKeys(t time.Time) []byte { - h := ghmac([]byte("AWS4"+s3.SecretKey), []byte(t.Format(shortTimeFormat))) - h = ghmac(h, []byte(s3.Region)) - h = ghmac(h, []byte(serviceName)) - h = ghmac(h, []byte("aws4_request")) + h := makeHMac([]byte("AWS4"+s3.SecretKey), []byte(t.Format(shortTimeFormat))) + h = makeHMac(h, []byte(s3.Region)) + h = makeHMac(h, []byte(serviceName)) + h = makeHMac(h, []byte("aws4_request")) return h } @@ -32,27 +31,27 @@ func (s3 *S3) writeRequest(w io.Writer, r *http.Request) { r.Header.Set("host", r.Host) w.Write([]byte(r.Method)) - w.Write(lf) + w.Write(newLine) writeURI(w, r) - w.Write(lf) + w.Write(newLine) writeQuery(w, r) - w.Write(lf) + w.Write(newLine) writeHeader(w, r) - w.Write(lf) - w.Write(lf) + w.Write(newLine) + w.Write(newLine) writeHeaderList(w, r) - w.Write(lf) + w.Write(newLine) writeBody(w, r) } func (s3 *S3) writeStringToSign(w io.Writer, t time.Time, r *http.Request) { w.Write([]byte("AWS4-HMAC-SHA256")) - w.Write(lf) + w.Write(newLine) w.Write([]byte(t.Format(amzDateISO8601TimeFormat))) - w.Write(lf) + w.Write(newLine) w.Write([]byte(s3.creds(t))) - w.Write(lf) + w.Write(newLine) h := sha256.New() s3.writeRequest(h, r) @@ -108,7 +107,7 @@ func writeHeader(w io.Writer, r *http.Request) { sort.Strings(a) for i, s := range a { if i > 0 { - w.Write(lf) + w.Write(newLine) } io.WriteString(w, s) } @@ -148,9 +147,3 @@ func writeBody(w io.Writer, r *http.Request) { h.Write(b) fmt.Fprintf(w, "%x", h.Sum(nil)) } - -func ghmac(key, data []byte) []byte { - h := hmac.New(sha256.New, key) - h.Write(data) - return h.Sum(nil) -}