-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrypto.go
176 lines (145 loc) · 3.34 KB
/
crypto.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package hawk
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"hash"
"net"
"net/url"
"strconv"
"strings"
)
const headerVersion = 1
type AuthType int
const (
Header AuthType = iota
Response
Bewit
)
type Mac struct {
Type AuthType
Credential *Credential
Uri string
Method string
HostPort string
Option *Option
}
type TsMac struct {
TimeStamp int64
Credential *Credential
}
type PayloadHash struct {
ContentType string
Payload string
Alg Alg
}
// String returns a base64 encoded message authentication code.
func (m *Mac) String() (string, error) {
digest, err := m.digest()
return base64.StdEncoding.EncodeToString(digest), err
}
func (m *Mac) digest() ([]byte, error) {
s := getHash(m.Credential.Alg)
mac := hmac.New(s, []byte(m.Credential.Key))
ns, err := m.normalized()
if err != nil {
return nil, err
}
mac.Write([]byte(ns))
return mac.Sum(nil), nil
}
func (m *Mac) normalized() (string, error) {
return normalized(m.Type, m.Uri, m.Method, m.HostPort, m.Option)
}
// String returns a base64 encoded message authentication code for timestamp
func (tm *TsMac) String() string {
digest := tm.digest()
return base64.StdEncoding.EncodeToString(digest)
}
func (tm *TsMac) digest() []byte {
s := getHash(tm.Credential.Alg)
mac := hmac.New(s, []byte(tm.Credential.Key))
ns := "hawk." + strconv.Itoa(headerVersion) + ".ts" + "\n" + strconv.FormatInt(tm.TimeStamp, 10) + "\n"
mac.Write([]byte(ns))
return mac.Sum(nil)
}
// String returns a base64 encoded hash value of payload
func (h *PayloadHash) String() string {
hash := h.hash()
return base64.StdEncoding.EncodeToString(hash)
}
func sanitizeContentType(contentType string) string {
return strings.TrimSpace(strings.ToLower(strings.Split(contentType, ";")[0]))
}
func (h *PayloadHash) hash() []byte {
s := getHash(h.Alg)()
ns := "hawk." + strconv.Itoa(headerVersion) + ".payload" + "\n" + sanitizeContentType(h.ContentType) + "\n" + h.Payload + "\n"
s.Write([]byte(ns))
return s.Sum(nil)
}
func normalized(authType AuthType, uri, method, customHost string, option *Option) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}
var h string
if customHost != "" {
h = customHost
} else {
h = u.Host
}
host, port, _ := net.SplitHostPort(h)
if port == "" {
switch u.Scheme {
case "http":
port = "80"
case "https":
port = "443"
}
}
if host == "" {
if customHost != "" {
host = customHost
} else {
host = u.Host
}
}
path := u.Path
if u.RawPath != "" {
path = u.RawPath
}
if u.Query().Encode() != "" {
path = path + "?" + u.RawQuery
}
header := "hawk" + "." + strconv.Itoa(headerVersion) + "." + strings.ToLower(authType.String())
ext := ""
if option.Ext != "" {
ext = strings.Replace(option.Ext, "\\", "\\\\", -1)
ext = strings.Replace(ext, "\n", "\\n", -1)
}
ns := header + "\n" +
strconv.FormatInt(option.TimeStamp, 10) + "\n" +
option.Nonce + "\n" +
strings.ToUpper(method) + "\n" +
path + "\n" +
strings.ToLower(host) + "\n" +
port + "\n" +
option.Hash + "\n" +
ext + "\n"
if option.App != "" {
ns = ns + option.App + "\n"
ns = ns + option.Dlg + "\n"
}
return ns, nil
}
func getHash(alg Alg) func() hash.Hash {
switch alg {
case SHA256:
return sha256.New
case SHA512:
return sha512.New
default:
return sha256.New
}
}