@@ -12,6 +12,7 @@ import (
12
12
"encoding/json"
13
13
"errors"
14
14
"fmt"
15
+ "hash"
15
16
"io/ioutil"
16
17
"log"
17
18
"math"
@@ -113,33 +114,30 @@ func ExtractCommaSeparatedValues(source, prefix string) []string {
113
114
return values
114
115
}
115
116
116
- func ExtractSignatures (signature , prefix string ) []string {
117
+ // ExtractSignatures will extract all the signatures from the source.
118
+ func ExtractSignatures (source , prefix string ) []string {
117
119
// If there are multiple possible matches, let the comma seperated extractor
118
120
// do it's work.
119
- if strings .Contains (signature , "," ) {
120
- return ExtractCommaSeparatedValues (signature , prefix )
121
+ if strings .Contains (source , "," ) {
122
+ return ExtractCommaSeparatedValues (source , prefix )
121
123
}
122
124
123
125
// There were no commas, so just trim the prefix (if it even exists) and
124
126
// pass it back.
125
127
return []string {
126
- strings .TrimPrefix (signature , prefix ),
128
+ strings .TrimPrefix (source , prefix ),
127
129
}
128
130
}
129
131
130
- // CheckPayloadSignature calculates and verifies SHA1 signature of the given payload
131
- func CheckPayloadSignature (payload []byte , secret string , signature string ) (string , error ) {
132
- if secret == "" {
133
- return "" , errors .New ("signature validation secret can not be empty" )
134
- }
135
-
136
- signatures := ExtractSignatures (signature , "sha1=" )
137
-
138
- mac := hmac .New (sha1 .New , []byte (secret ))
132
+ // ValidateMAC will verify that the expected mac for the given hash will match
133
+ // the one provided.
134
+ func ValidateMAC (payload []byte , mac hash.Hash , signatures []string ) (string , error ) {
135
+ // Write the payload to the provided hash.
139
136
_ , err := mac .Write (payload )
140
137
if err != nil {
141
138
return "" , err
142
139
}
140
+
143
141
expectedMAC := hex .EncodeToString (mac .Sum (nil ))
144
142
145
143
for _ , signature := range signatures {
@@ -153,25 +151,30 @@ func CheckPayloadSignature(payload []byte, secret string, signature string) (str
153
151
}
154
152
}
155
153
154
+ // CheckPayloadSignature calculates and verifies SHA1 signature of the given payload
155
+ func CheckPayloadSignature (payload []byte , secret string , signature string ) (string , error ) {
156
+ if secret == "" {
157
+ return "" , errors .New ("signature validation secret can not be empty" )
158
+ }
159
+
160
+ // Extract the signatures.
161
+ signatures := ExtractSignatures (signature , "sha1=" )
162
+
163
+ // Validate the MAC.
164
+ return ValidateMAC (payload , hmac .New (sha1 .New , []byte (secret )), signatures )
165
+ }
166
+
156
167
// CheckPayloadSignature256 calculates and verifies SHA256 signature of the given payload
157
168
func CheckPayloadSignature256 (payload []byte , secret string , signature string ) (string , error ) {
158
169
if secret == "" {
159
170
return "" , errors .New ("signature validation secret can not be empty" )
160
171
}
161
172
173
+ // Extract the signatures.
162
174
signatures := ExtractSignatures (signature , "sha256=" )
163
175
164
- mac := hmac .New (sha256 .New , []byte (secret ))
165
- _ , err := mac .Write (payload )
166
- if err != nil {
167
- return "" , err
168
- }
169
- expectedMAC := hex .EncodeToString (mac .Sum (nil ))
170
-
171
- if ! hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
172
- return expectedMAC , & SignatureError {signature }
173
- }
174
- return expectedMAC , err
176
+ // Validate the MAC.
177
+ return ValidateMAC (payload , hmac .New (sha256 .New , []byte (secret )), signatures )
175
178
}
176
179
177
180
// CheckPayloadSignature512 calculates and verifies SHA512 signature of the given payload
@@ -180,24 +183,11 @@ func CheckPayloadSignature512(payload []byte, secret string, signature string) (
180
183
return "" , errors .New ("signature validation secret can not be empty" )
181
184
}
182
185
183
- signature = strings .TrimPrefix (signature , "sha512=" )
184
-
185
- mac := hmac .New (sha512 .New , []byte (secret ))
186
- _ , err := mac .Write (payload )
187
- if err != nil {
188
- return "" , err
189
- }
190
- expectedMAC := hex .EncodeToString (mac .Sum (nil ))
191
-
192
- for _ , signature := range signatures {
193
- if hmac .Equal ([]byte (signature ), []byte (expectedMAC )) {
194
- return expectedMAC , err
195
- }
196
- }
186
+ // Extract the signatures.
187
+ signatures := ExtractSignatures (signature , "sha512=" )
197
188
198
- return expectedMAC , & SignatureError {
199
- Signatures : signatures ,
200
- }
189
+ // Validate the MAC.
190
+ return ValidateMAC (payload , hmac .New (sha512 .New , []byte (secret )), signatures )
201
191
}
202
192
203
193
func CheckScalrSignature (headers map [string ]interface {}, body []byte , signingKey string , checkDate bool ) (bool , error ) {
0 commit comments