File: md5.go

package info (click to toggle)
golang-github-docker-goamz 0.0~git20160206.0.f0a21f5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,580 kB
  • sloc: makefile: 66
file content (61 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package sqs

/*
 * Performs the MD5 algorithm for attribute responses described in
 * the AWS documentation here: http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSMessageAttributes.html#sqs-attrib-md5
 */

import (
	"crypto/md5"
	"encoding/binary"
	"sort"
)

// Returns length of string as an Big Endian byte array
func getStringLengthAsByteArray(s string) []byte {
	var res []byte = make([]byte, 4)
	binary.BigEndian.PutUint32(res, uint32(len(s)))

	return res
}

// How to calculate the MD5 of Attributes
func calculateAttributeMD5(attributes map[string]string) []byte {

	// We're going to walk attributes in alpha-sorted order
	var keys []string

	for k := range attributes {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	// Now we'll build our encoded string
	var encoded []byte

	for _, k := range keys {
		v := attributes[k]
		t := "String"

		encodedItems := [][]byte{
			getStringLengthAsByteArray(k),
			[]byte(k), // Name
			getStringLengthAsByteArray(t),
			[]byte(t), // Data Type ("String")
			{0x01},    // "String Value" (0x01)
			getStringLengthAsByteArray(v),
			[]byte(v), // Value
		}

		// Append each of these to our encoding
		for _, item := range encodedItems {
			encoded = append(encoded, item...)
		}
	}

	res := md5.Sum(encoded)

	// Return MD5 sum
	return res[:]
}