-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigner.go
More file actions
51 lines (43 loc) · 939 Bytes
/
signer.go
File metadata and controls
51 lines (43 loc) · 939 Bytes
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
package openapi
import (
"crypto/sha256"
"fmt"
"sort"
"strings"
)
// Sign with sha 256
func Sign(content, key string) string {
h := sha256.New()
h.Write([]byte(content + key))
return fmt.Sprintf("%x", h.Sum(nil))
}
func verify(result, content, key string) bool {
signed := Sign(content, key)
return strings.EqualFold(signed, result)
}
// KvPair is a simple struct for kv pair
type KvPair struct {
Key string `json:"key"`
Value string `json:"value"`
}
// Pairs is the slice of the KvPair
type Pairs []KvPair
func (p Pairs) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p Pairs) Len() int {
return len(p)
}
func (p Pairs) Less(i, j int) bool {
return p[i].Key < p[j].Key
}
// 防止存放攻击, 客户端需要显式的传过来时间戳
func buildParams(params Pairs) string {
sort.Sort(params)
var result string
for _, v := range params {
r := v.Key + "=" + v.Value + "&"
result += r
}
return result
}