aws-http-auth/internal/v4/strings.go (47 lines of code) (raw):
package v4
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
)
var noEscape [256]bool
func init() {
for i := 0; i < len(noEscape); i++ {
// AWS expects every character except these to be escaped
noEscape[i] = (i >= 'A' && i <= 'Z') ||
(i >= 'a' && i <= 'z') ||
(i >= '0' && i <= '9') ||
i == '-' ||
i == '.' ||
i == '_' ||
i == '~' ||
i == '/'
}
}
// uriEncode implements "Amazon-style" URL escaping.
func uriEncode(path string) string {
var buf bytes.Buffer
for i := 0; i < len(path); i++ {
c := path[i]
if noEscape[c] {
buf.WriteByte(c)
} else {
fmt.Fprintf(&buf, "%%%02X", c)
}
}
return buf.String()
}
// rtosha computes the sha256 hash of the input Reader and rewinds it before
// returning.
func rtosha(r io.ReadSeeker) ([]byte, error) {
h := sha256.New()
if _, err := io.Copy(h, r); err != nil {
return nil, err
}
if _, err := r.Seek(0, io.SeekStart); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// Stosha computes the sha256 hash of the given string.
func Stosha(s string) []byte {
h := sha256.New()
h.Write([]byte(s))
return h.Sum(nil)
}