forked from storezhang/echox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signature.go
72 lines (65 loc) · 1.56 KB
/
signature.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package echox
import (
`fmt`
`github.com/labstack/echo/v4/middleware`
`github.com/storezhang/gox`
)
// 支持的算法
var supportAlgorithm = []Algorithm{
// 对称加密算法
HmacWithSHA224,
HmacWithSHA256,
HmacWithSHA384,
HmacWithSHA512,
HmacWithRipemd160,
HmacWithSHA3224,
HmacWithSHA3256,
HmacWithSHA3384,
HmacWithSHA3512,
HmacWithSHA512224,
HmacWithSHA512256,
HmacWithBlake2s256,
HmacWithBlake2b256,
HmacWithBlake2b384,
HmacWithBlake2b512,
Blake2sWith256,
Blake2bWith256,
Blake2bWith384,
Blake2bWith512,
// RAS非对称加密算法
RsaWithSHA224,
RsaWithSHA256,
RsaWithSHA384,
RsaWithSHA512,
RsaWithRipemd160,
// ECDSA非对称加密算法
EcdsaWithSHA224,
EcdsaWithSHA256,
EcdsaWithSHA384,
EcdsaWithSHA512,
EcdsaWithRipemd160,
}
type Signature struct {
// 确定是不是要走中间件
skipper middleware.Skipper `validate:"required"`
// 签名算法
algorithm Algorithm `validate:"required"`
// 获得签名参数
source keySource `validate:"required"`
}
// NewSignature Http签名
func NewSignature(algorithm Algorithm, source keySource) *Signature {
return NewSignatureWithConfig(middleware.DefaultSkipper, algorithm, source)
}
// NewSignatureWithConfig Http签名
func NewSignatureWithConfig(skipper middleware.Skipper, algorithm Algorithm, source keySource) *Signature {
// 检查算法配置是否正确
if exists, _ := gox.IsInArray(algorithm, supportAlgorithm); !exists {
panic(fmt.Errorf("不支持的算法:%s", algorithm))
}
return &Signature{
skipper: skipper,
algorithm: algorithm,
source: source,
}
}