-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.go
40 lines (31 loc) · 842 Bytes
/
algorithm.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
package itsdangerous
import (
"crypto/hmac"
"crypto/sha1"
"hash"
)
type SigningAlgorithm interface {
GetSignature(key, value []byte) []byte
VerifySignature(key, value, sig []byte) bool
}
type NoneAlgorithm struct{}
func (alg *NoneAlgorithm) GetSignature(_, _ []byte) []byte {
return []byte{'b'}
}
func (alg *NoneAlgorithm) VerifySignature(key, value, sig []byte) bool {
return hmac.Equal(alg.GetSignature(key, value), sig)
}
type HMACAlgorithm struct {
DigestMethod func() hash.Hash
}
func (alg *HMACAlgorithm) GetSignature(key, value []byte) []byte {
if alg.DigestMethod == nil {
alg.DigestMethod = sha1.New
}
mac := hmac.New(alg.DigestMethod, key)
mac.Write(value)
return mac.Sum(nil)
}
func (alg *HMACAlgorithm) VerifySignature(key, value, sig []byte) bool {
return hmac.Equal(alg.GetSignature(key, value), sig)
}