gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

utils.go (1874B)


      1 /*
      2  * MinIO Go Library for Amazon S3 Compatible Cloud Storage
      3  * Copyright 2015-2017 MinIO, Inc.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package signer
     19 
     20 import (
     21 	"crypto/hmac"
     22 	"crypto/sha256"
     23 	"net/http"
     24 	"strings"
     25 )
     26 
     27 // unsignedPayload - value to be set to X-Amz-Content-Sha256 header when
     28 const unsignedPayload = "UNSIGNED-PAYLOAD"
     29 
     30 // sum256 calculate sha256 sum for an input byte array.
     31 func sum256(data []byte) []byte {
     32 	hash := sha256.New()
     33 	hash.Write(data)
     34 	return hash.Sum(nil)
     35 }
     36 
     37 // sumHMAC calculate hmac between two input byte array.
     38 func sumHMAC(key []byte, data []byte) []byte {
     39 	hash := hmac.New(sha256.New, key)
     40 	hash.Write(data)
     41 	return hash.Sum(nil)
     42 }
     43 
     44 // getHostAddr returns host header if available, otherwise returns host from URL
     45 func getHostAddr(req *http.Request) string {
     46 	host := req.Header.Get("host")
     47 	if host != "" && req.Host != host {
     48 		return host
     49 	}
     50 	if req.Host != "" {
     51 		return req.Host
     52 	}
     53 	return req.URL.Host
     54 }
     55 
     56 // Trim leading and trailing spaces and replace sequential spaces with one space, following Trimall()
     57 // in http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
     58 func signV4TrimAll(input string) string {
     59 	// Compress adjacent spaces (a space is determined by
     60 	// unicode.IsSpace() internally here) to one space and return
     61 	return strings.Join(strings.Fields(input), " ")
     62 }