~prudhvikrishna/sqs/trunk

« back to all changes in this revision

Viewing changes to sign.go

  • Committer: Prudhvi Krishna Surapaneni
  • Date: 2012-05-24 21:06:40 UTC
  • Revision ID: me@prudhvi.net-20120524210640-rt0mcxlp42v5kwze
Import SQS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package sqs
 
2
 
 
3
import (
 
4
        "crypto/hmac"
 
5
        "crypto/sha256"
 
6
        "encoding/base64"
 
7
        "launchpad.net/goamz/aws"
 
8
        "sort"
 
9
        "strings"
 
10
)
 
11
 
 
12
// ----------------------------------------------------------------------------
 
13
// EC2 signing (http://goo.gl/fQmAN)
 
14
 
 
15
var b64 = base64.StdEncoding
 
16
 
 
17
func sign(auth aws.Auth, method, path string, params map[string]string, host string) {
 
18
        params["AWSAccessKeyId"] = auth.AccessKey
 
19
        params["SignatureVersion"] = "2"
 
20
        params["SignatureMethod"] = "HmacSHA256"
 
21
 
 
22
        var sarray []string
 
23
        for k, v := range params {
 
24
                sarray = append(sarray, aws.Encode(k)+"="+aws.Encode(v))
 
25
        }
 
26
        sort.StringSlice(sarray).Sort()
 
27
        joined := strings.Join(sarray, "&")
 
28
        payload := method + "\n" + host + "\n" + path + "\n" + joined
 
29
        hash := hmac.New(sha256.New, []byte(auth.SecretKey))
 
30
        hash.Write([]byte(payload))
 
31
        signature := make([]byte, b64.EncodedLen(hash.Size()))
 
32
        b64.Encode(signature, hash.Sum(nil))
 
33
 
 
34
        params["Signature"] = string(signature)
 
35
}