-
Notifications
You must be signed in to change notification settings - Fork 0
/
keccak_test.go
48 lines (43 loc) · 1.29 KB
/
keccak_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
package crypto
import (
"reflect"
"testing"
)
var keccakTestCases = []struct {
hasher Hasher
input [][]byte
expected string
}{
{&Keccak256Hasher{}, nil, "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},
{&Keccak256Hasher{}, [][]byte{{}}, "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},
{&Keccak256Hasher{}, [][]byte{[]byte("asdf")}, "4c8f18581c0167eb90a761b4a304e009b924f03b619a0c0e8ea3adfce20aee64"},
{&Keccak256Hasher{}, [][]byte{[]byte("asdf"), []byte("qwer")}, "68432ece0d2ea60cf463a6a9fae1b6deef509cb1e3b422df729ea6cc418ee8b5"},
}
func TestKeccakHasherHex(t *testing.T) {
for _, tt := range keccakTestCases {
t.Run(tt.expected, func(t *testing.T) {
result := tt.hasher.HashHex(tt.input...)
if result != tt.expected {
t.Errorf("Got %s, want %s", result, tt.expected)
}
})
}
}
func TestKeccakHasherHash(t *testing.T) {
for _, tt := range keccakTestCases {
t.Run(tt.expected, func(t *testing.T) {
result := tt.hasher.Hash(tt.input...)
expected, _ := FromHex(tt.expected)
if !reflect.DeepEqual(expected, result) {
t.Errorf("Got %s, want %s", result, expected)
}
})
}
}
func BenchmarkKeccak256(b *testing.B) {
hasher := Keccak256Hasher{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
hasher.Hash([]byte("qwerty"))
}
}