-
Notifications
You must be signed in to change notification settings - Fork 31
/
hmac.go
37 lines (32 loc) · 853 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
37
package cryptography
import (
"crypto/hmac"
"crypto/sha512"
"errors"
)
// HMACKey an interface for hashing to hmac-sha384
type HMACKey interface {
// HMACSha384 does the appropriate hashing
HMACSha384(payload []byte) ([]byte, error)
}
// HMACHasher is an in process signer implementation for HMACKey
type HMACHasher struct {
secret []byte
}
// NewHMACHasher creates a new HMACKey for hashing
func NewHMACHasher(secret []byte) HMACKey {
hasher := HMACHasher{secret}
return &hasher
}
// HMACSha384 hashes using an in process secret
func (hmh *HMACHasher) HMACSha384(payload []byte) ([]byte, error) {
mac := hmac.New(sha512.New384, hmh.secret)
len, err := mac.Write([]byte(payload))
if err != nil {
return []byte{}, err
}
if len == 0 {
return []byte{}, errors.New("no bytes written in HMACSha384 Hash")
}
return mac.Sum(nil), nil
}