-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.go
More file actions
32 lines (27 loc) · 862 Bytes
/
algorithm.go
File metadata and controls
32 lines (27 loc) · 862 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
package itsdangerous
import (
"crypto/hmac"
"hash"
)
// SigningAlgorithm provides interfaces to generate and verify signature
type SigningAlgorithm interface {
GetSignature(key []byte, value string) []byte
VerifySignature(key []byte, value string, signature []byte) bool
}
// HMACAlgorithm provides signature generation using HMACs.
type HMACAlgorithm struct {
DigestMethod func() hash.Hash
}
// GetSignature returns the signature for the given key and value.
func (a *HMACAlgorithm) GetSignature(key []byte, value string) []byte {
h := hmac.New(a.DigestMethod, key)
h.Write([]byte(value))
return h.Sum(nil)
}
// VerifySignature verifies the given signature matches the expected signature.
func (a *HMACAlgorithm) VerifySignature(key []byte, value string, signature []byte) bool {
return hmac.Equal(
signature,
a.GetSignature(key, value),
)
}