-
Notifications
You must be signed in to change notification settings - Fork 188
/
executor.go
58 lines (50 loc) · 1.33 KB
/
executor.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
package hmac_sha256
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"strings"
"github.com/eolinker/eosc/log"
dynamic_params "github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params"
http_service "github.com/eolinker/eosc/eocontext/http-context"
)
type executor struct {
*dynamic_params.Param
secretKey string
}
func NewExecutor(name string, value []string) *executor {
secretKey := ""
v := value
if len(value) >= 1 {
secretKey = value[0]
v = v[1:]
}
return &executor{
Param: dynamic_params.NewParam(name, v),
secretKey: secretKey,
}
}
func (m *executor) Generate(ctx http_service.IHttpContext, contentType string, args ...interface{}) (interface{}, error) {
result, err := m.Param.Generate(ctx, contentType, args...)
if err != nil {
return nil, err
}
v, ok := result.(string)
if !ok {
return nil, errors.New("hmac-sha256 value is not string")
}
value := hMacBySHA256(m.secretKey, v)
if !strings.HasPrefix(m.Name(), "__") {
value = strings.ToUpper(value)
}
log.DebugF("hmac-sha256 value before: %s,after: %s", v, value)
return value, nil
}
func hMacBySHA256(secretKey, toSign string) string {
// 创建对应的sha256哈希加密算法
hm := hmac.New(sha256.New, []byte(secretKey))
//写入加密数据
hm.Write([]byte(toSign))
return hex.EncodeToString(hm.Sum(nil))
}