forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
42 lines (35 loc) · 1010 Bytes
/
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
package pusher
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
// "fmt"
"strings"
)
func hmacSignature(toSign, secret string) string {
return hex.EncodeToString(hmacBytes([]byte(toSign), []byte(secret)))
}
func hmacBytes(toSign, secret []byte) []byte {
_authSignature := hmac.New(sha256.New, secret)
_authSignature.Write(toSign)
return _authSignature.Sum(nil)
}
func checkSignature(result, secret string, body []byte) bool {
expected := hmacBytes(body, []byte(secret))
resultBytes, err := hex.DecodeString(result)
if err != nil {
return false
}
return hmac.Equal(expected, resultBytes)
}
func createAuthMap(key, secret, stringToSign string) map[string]string {
authSignature := hmacSignature(stringToSign, secret)
authString := strings.Join([]string{key, authSignature}, ":")
return map[string]string{"auth": authString}
}
func md5Signature(body []byte) string {
_bodyMD5 := md5.New()
_bodyMD5.Write([]byte(body))
return hex.EncodeToString(_bodyMD5.Sum(nil))
}