forked from abiosoft/caddy-hmac
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.go
39 lines (32 loc) · 744 Bytes
/
hash.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
package hmac
import (
"crypto/hmac"
"encoding/hex"
"hash"
)
type hashAlgorithm string
func (h hashAlgorithm) valid() bool {
switch h {
case algSha1:
case algSha256:
case algMd5:
default:
return false
}
return true
}
// hash algorithms
const (
algSha1 hashAlgorithm = "sha1"
algSha256 hashAlgorithm = "sha256"
algMd5 hashAlgorithm = "md5"
)
// generateSignature generates hmac signature using the hasher, secret
// and bytes.
func generateSignature(hasher func() hash.Hash, secret string, b []byte) string {
h := hmac.New(hasher, []byte(secret))
// no error check needed, never returns an error
// https://github.com/golang/go/blob/go1.14.3/src/hash/hash.go#L28
h.Write(b)
return hex.EncodeToString(h.Sum(nil))
}