-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhash_test.go
119 lines (110 loc) · 4 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
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util_test
import (
"testing"
"github.com/stretchr/testify/assert"
util "github.com/wealdtech/go-eth2-util"
)
func TestSHA256(t *testing.T) {
tests := []struct {
name string
input [][]byte
hash []byte
}{
{
name: "Nil",
input: nil,
hash: []byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
},
{
name: "Empty",
input: [][]byte{},
hash: []byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
},
{
name: "DoubleEmpty",
input: [][]byte{{}, {}},
hash: []byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
},
{
name: "Data",
input: [][]byte{{0x01}},
hash: []byte{0x4b, 0xf5, 0x12, 0x2f, 0x34, 0x45, 0x54, 0xc5, 0x3b, 0xde, 0x2e, 0xbb, 0x8c, 0xd2, 0xb7, 0xe3, 0xd1, 0x60, 0x0a, 0xd6, 0x31, 0xc3, 0x85, 0xa5, 0xd7, 0xcc, 0xe2, 0x3c, 0x77, 0x85, 0x45, 0x9a},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
hash := util.SHA256(test.input...)
assert.Equal(t, test.hash, hash)
})
}
}
func TestSHA3256(t *testing.T) {
tests := []struct {
name string
input []byte
hash []byte
}{
{
name: "Nil",
input: nil,
hash: []byte{0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66, 0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62, 0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa, 0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a},
},
{
name: "Empty",
input: []byte{},
hash: []byte{0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66, 0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62, 0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa, 0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a},
},
{
name: "Data",
input: []byte{0x01},
hash: []byte{0x27, 0x67, 0xf1, 0x5c, 0x8a, 0xf2, 0xf2, 0xc7, 0x22, 0x5d, 0x52, 0x73, 0xfd, 0xd6, 0x83, 0xed, 0xc7, 0x14, 0x11, 0x0a, 0x98, 0x7d, 0x10, 0x54, 0x69, 0x7c, 0x34, 0x8a, 0xed, 0x4e, 0x6c, 0xc7},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
hash := util.SHA3256(test.input)
assert.Equal(t, test.hash, hash)
})
}
}
func TestKeccak256(t *testing.T) {
tests := []struct {
name string
input []byte
hash []byte
}{
{
name: "Nil",
input: nil,
hash: []byte{0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70},
},
{
name: "Empty",
input: []byte{},
hash: []byte{0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70},
},
{
name: "Data",
input: []byte{0x01},
hash: []byte{0x5f, 0xe7, 0xf9, 0x77, 0xe7, 0x1d, 0xba, 0x2e, 0xa1, 0xa6, 0x8e, 0x21, 0x05, 0x7b, 0xee, 0xbb, 0x9b, 0xe2, 0xac, 0x30, 0xc6, 0x41, 0x0a, 0xa3, 0x8d, 0x4f, 0x3f, 0xbe, 0x41, 0xdc, 0xff, 0xd2},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
hash := util.Keccak256(test.input)
assert.Equal(t, test.hash, hash)
})
}
}