-
Notifications
You must be signed in to change notification settings - Fork 9
/
hash_test.go
229 lines (217 loc) · 4.82 KB
/
hash_test.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package openssl_test
import (
"bytes"
"crypto"
"encoding"
"hash"
"io"
"testing"
"github.com/golang-fips/openssl/v2"
)
func cryptoToHash(h crypto.Hash) func() hash.Hash {
switch h {
case crypto.MD4:
return openssl.NewMD4
case crypto.MD5:
return openssl.NewMD5
case crypto.SHA1:
return openssl.NewSHA1
case crypto.SHA224:
return openssl.NewSHA224
case crypto.SHA256:
return openssl.NewSHA256
case crypto.SHA384:
return openssl.NewSHA384
case crypto.SHA512:
return openssl.NewSHA512
case crypto.SHA3_224:
return openssl.NewSHA3_224
case crypto.SHA3_256:
return openssl.NewSHA3_256
case crypto.SHA3_384:
return openssl.NewSHA3_384
case crypto.SHA3_512:
return openssl.NewSHA3_512
}
return nil
}
func TestHash(t *testing.T) {
msg := []byte("testing")
var tests = []struct {
h crypto.Hash
hasMarshaler bool
}{
{crypto.MD4, false},
{crypto.MD5, true},
{crypto.SHA1, true},
{crypto.SHA224, true},
{crypto.SHA256, true},
{crypto.SHA384, true},
{crypto.SHA512, true},
{crypto.SHA3_224, false},
{crypto.SHA3_256, false},
{crypto.SHA3_384, false},
{crypto.SHA3_512, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.h.String(), func(t *testing.T) {
t.Parallel()
if !openssl.SupportsHash(tt.h) {
t.Skip("skipping: not supported")
}
h := cryptoToHash(tt.h)()
initSum := h.Sum(nil)
n, err := h.Write(msg)
if err != nil {
t.Fatal(err)
}
if n != len(msg) {
t.Errorf("got: %d, want: %d", n, len(msg))
}
sum := h.Sum(nil)
if size := h.Size(); len(sum) != size {
t.Errorf("got: %d, want: %d", len(sum), size)
}
if bytes.Equal(sum, initSum) {
t.Error("Write didn't change internal hash state")
}
if tt.hasMarshaler {
state, err := h.(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
t.Errorf("could not marshal: %v", err)
}
h2 := cryptoToHash(tt.h)()
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
}
if actual, actual2 := h.Sum(nil), h2.Sum(nil); !bytes.Equal(actual, actual2) {
t.Errorf("0x%x != marshaled 0x%x", actual, actual2)
}
}
h.Reset()
sum = h.Sum(nil)
if !bytes.Equal(sum, initSum) {
t.Errorf("got:%x want:%x", sum, initSum)
}
bw := h.(io.ByteWriter)
for i := 0; i < len(msg); i++ {
bw.WriteByte(msg[i])
}
h.Reset()
sum = h.Sum(nil)
if !bytes.Equal(sum, initSum) {
t.Errorf("got:%x want:%x", sum, initSum)
}
h.(io.StringWriter).WriteString(string(msg))
h.Reset()
sum = h.Sum(nil)
if !bytes.Equal(sum, initSum) {
t.Errorf("got:%x want:%x", sum, initSum)
}
})
}
}
func TestHash_OneShot(t *testing.T) {
msg := []byte("testing")
var tests = []struct {
h crypto.Hash
oneShot func([]byte) []byte
}{
{crypto.SHA1, func(p []byte) []byte {
b := openssl.SHA1(p)
return b[:]
}},
{crypto.SHA224, func(p []byte) []byte {
b := openssl.SHA224(p)
return b[:]
}},
{crypto.SHA256, func(p []byte) []byte {
b := openssl.SHA256(p)
return b[:]
}},
{crypto.SHA384, func(p []byte) []byte {
b := openssl.SHA384(p)
return b[:]
}},
{crypto.SHA512, func(p []byte) []byte {
b := openssl.SHA512(p)
return b[:]
}},
{crypto.SHA3_224, func(p []byte) []byte {
b := openssl.SHA3_224(p)
return b[:]
}},
{crypto.SHA3_256, func(p []byte) []byte {
b := openssl.SHA3_256(p)
return b[:]
}},
{crypto.SHA3_384, func(p []byte) []byte {
b := openssl.SHA3_384(p)
return b[:]
}},
{crypto.SHA3_512, func(p []byte) []byte {
b := openssl.SHA3_512(p)
return b[:]
}},
}
for _, tt := range tests {
t.Run(tt.h.String(), func(t *testing.T) {
if !openssl.SupportsHash(tt.h) {
t.Skip("skipping: not supported")
}
got := tt.oneShot(msg)
h := cryptoToHash(tt.h)()
h.Write(msg)
want := h.Sum(nil)
if !bytes.Equal(got[:], want) {
t.Errorf("got:%x want:%x", got, want)
}
})
}
}
type cgoData struct {
Data [16]byte
Ptr *cgoData
}
func TestCgo(t *testing.T) {
// Test that Write does not cause cgo to scan the entire cgoData struct for pointers.
// The scan (if any) should be limited to the [16]byte.
defer func() {
if err := recover(); err != nil {
t.Error(err)
}
}()
d := new(cgoData)
d.Ptr = d
h := openssl.NewSHA256()
h.Write(d.Data[:])
h.Sum(nil)
openssl.SHA256(d.Data[:])
}
func BenchmarkHash8Bytes(b *testing.B) {
b.StopTimer()
h := openssl.NewSHA256()
sum := make([]byte, h.Size())
size := 8
buf := make([]byte, size)
b.StartTimer()
b.SetBytes(int64(size))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
h.Reset()
h.Write(buf)
h.Sum(sum[:0])
}
}
func BenchmarkSHA256(b *testing.B) {
b.StopTimer()
size := 8
buf := make([]byte, size)
b.StartTimer()
b.SetBytes(int64(size))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
openssl.SHA256(buf)
}
}