-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
56 lines (48 loc) · 1.25 KB
/
pool.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package hmacr
import (
"hash"
"sync"
)
// Pool is a pool of HMAC resources for a specific hash.
type Pool struct {
pool sync.Pool
}
// NewPool creates a pool of HMAC resources with specified hash, h.
func NewPool(h func() hash.Hash) *Pool {
return &Pool{sync.Pool{New: func() interface{} { return New(h, nil) }}}
}
// Get retrieves a HMAC from pool of resources and initialised with given key.
func (pool *Pool) Get(key []byte) HMAC {
r := pool.pool.Get().(HMAC)
r.SetKey(key)
return r
}
// Put release a MAC back to the pool. Attempts to scrub the key used.
func (pool *Pool) Put(m HMAC) {
m.ClearKey()
pool.pool.Put(m)
}
// HMAC using pooled HMAC resources.
func (pool *Pool) HMAC(in, key []byte, data ...[]byte) []byte {
mac := pool.Get(key)
for _, d := range data {
mac.Write(d)
}
in = mac.Sum(in)
pool.Put(mac)
return in
}
// HKDF using pooled HMAC resources.
func (pool *Pool) HKDF(p, secret, salt, info []byte) (int, error) {
mac := pool.Get(salt)
n, err := hkdfIntern(mac, p, secret, info)
pool.Put(mac)
return n, err
}
// PBKDF2 using pooled HMAC resources.
func (pool *Pool) PBKDF2(p, password, salt []byte, iter int) (int, error) {
mac := pool.Get(password)
n, err := pbkdf2Intern(mac, p, salt, iter)
pool.Put(mac)
return n, err
}