-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmac.go
36 lines (30 loc) · 811 Bytes
/
hmac.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
package hasher
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
)
// HmacMd5 returns a new HMAC hash using md5.
func HmacMd5(data []byte, secret string) Hash {
return mac(data, secret, md5.New)
}
// HmacSha1 returns a new HMAC hash using sha1.
func HmacSha1(data []byte, secret string) Hash {
return mac(data, secret, sha1.New)
}
// HmacSha256 returns a new HMAC hash using sha256.
func HmacSha256(data []byte, secret string) Hash {
return mac(data, secret, sha256.New)
}
// HmacSha512 returns a new HMAC hash using sha512.
func HmacSha512(data []byte, secret string) Hash {
return mac(data, secret, sha512.New)
}
func mac(data []byte, secret string, a func() hash.Hash) Hash {
h := hmac.New(a, []byte(secret))
_, _ = h.Write(data)
return h.Sum(nil)
}