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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package hashenc
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"sync"
)
// Hash defines a pooled hash.Hash implementation
type Hash interface {
// Hash ensures we implement the base hash.Hash implementation
hash.Hash
// Release resets the Hash and places it back in the pool
Release()
}
// poolHash is our Hash implementation, providing a hash.Hash and a pool to return to
type poolHash struct {
hash.Hash
pool *sync.Pool
}
func (h *poolHash) Release() {
h.Reset()
h.pool.Put(h)
}
// SHA512Pool defines a pool of SHA512 hashes
type SHA512Pool interface {
// SHA512 returns a Hash implementing the SHA512 hashing algorithm
SHA512() Hash
}
// NewSHA512Pool returns a new SHA512Pool implementation
func NewSHA512Pool() SHA512Pool {
p := &sha512Pool{}
p.New = func() interface{} {
return &poolHash{
Hash: sha512.New(),
pool: &p.Pool,
}
}
return p
}
// sha512Pool is our SHA512Pool implementation, simply wrapping sync.Pool
type sha512Pool struct {
sync.Pool
}
func (p *sha512Pool) SHA512() Hash {
return p.Get().(Hash)
}
// SHA256Pool defines a pool of SHA256 hashes
type SHA256Pool interface {
// SHA256 returns a Hash implementing the SHA256 hashing algorithm
SHA256() Hash
}
// NewSHA256Pool returns a new SHA256Pool implementation
func NewSHA256Pool() SHA256Pool {
p := &sha256Pool{}
p.New = func() interface{} {
return &poolHash{
Hash: sha256.New(),
pool: &p.Pool,
}
}
return p
}
// sha256Pool is our SHA256Pool implementation, simply wrapping sync.Pool
type sha256Pool struct {
sync.Pool
}
func (p *sha256Pool) SHA256() Hash {
return p.Get().(Hash)
}
// SHA1Pool defines a pool of SHA1 hashes
type SHA1Pool interface {
SHA1() Hash
}
// NewSHA1Pool returns a new SHA1Pool implementation
func NewSHA1Pool() SHA1Pool {
p := &sha1Pool{}
p.New = func() interface{} {
return &poolHash{
Hash: sha1.New(),
pool: &p.Pool,
}
}
return p
}
// sha1Pool is our SHA1Pool implementation, simply wrapping sync.Pool
type sha1Pool struct {
sync.Pool
}
func (p *sha1Pool) SHA1() Hash {
return p.Get().(Hash)
}
// MD5Pool defines a pool of MD5 hashes
type MD5Pool interface {
MD5() Hash
}
// NewMD5Pool returns a new MD5 implementation
func NewMD5Pool() MD5Pool {
p := &md5Pool{}
p.New = func() interface{} {
return &poolHash{
Hash: md5.New(),
pool: &p.Pool,
}
}
return p
}
// md5Pool is our MD5Pool implementation, simply wrapping sync.Pool
type md5Pool struct {
sync.Pool
}
func (p *md5Pool) MD5() Hash {
return p.Get().(Hash)
}
|