-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.go
49 lines (40 loc) · 1.07 KB
/
helper.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
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"os"
"strings"
)
func getFileContent(path string) (conten []byte, err error) {
jf, err := os.Open(path)
if err != nil {
return nil, err
}
defer jf.Close()
data, _ := ioutil.ReadAll(jf)
return data, nil
}
func makeNewSignature(key, content []byte) string {
dst := make([]byte, 40)
computed := hmac.New(sha1.New, key)
computed.Write(content)
hex.Encode(dst, computed.Sum(nil))
return "sha1=" + string(dst)
}
func computedBodySignature(key, content []byte) []byte {
computed := hmac.New(sha1.New, key)
computed.Write(content)
return []byte(computed.Sum(nil))
}
func checkContentSignature(content []byte, signature string, body []byte) bool {
const signaturePrefix = "sha1="
const signatureLength = 45 // len(SignaturePrefix) + len(hex(sha1))
if len(signature) != signatureLength || !strings.HasPrefix(signature, signaturePrefix) {
return false
}
actual := make([]byte, 20)
hex.Decode(actual, []byte(signature[5:]))
return hmac.Equal(computedBodySignature(content, body), actual)
}