1
2
3
4
5
6
7 package sha3
8
9 import (
10 "crypto"
11 "crypto/internal/fips140/sha3"
12 "hash"
13 )
14
15 func init() {
16 crypto.RegisterHash(crypto.SHA3_224, func() hash.Hash { return New224() })
17 crypto.RegisterHash(crypto.SHA3_256, func() hash.Hash { return New256() })
18 crypto.RegisterHash(crypto.SHA3_384, func() hash.Hash { return New384() })
19 crypto.RegisterHash(crypto.SHA3_512, func() hash.Hash { return New512() })
20 }
21
22
23 func Sum224(data []byte) [28]byte {
24 var out [28]byte
25 h := sha3.New224()
26 h.Write(data)
27 h.Sum(out[:0])
28 return out
29 }
30
31
32 func Sum256(data []byte) [32]byte {
33 var out [32]byte
34 h := sha3.New256()
35 h.Write(data)
36 h.Sum(out[:0])
37 return out
38 }
39
40
41 func Sum384(data []byte) [48]byte {
42 var out [48]byte
43 h := sha3.New384()
44 h.Write(data)
45 h.Sum(out[:0])
46 return out
47 }
48
49
50 func Sum512(data []byte) [64]byte {
51 var out [64]byte
52 h := sha3.New512()
53 h.Write(data)
54 h.Sum(out[:0])
55 return out
56 }
57
58
59
60 func SumSHAKE128(data []byte, length int) []byte {
61
62 out := make([]byte, 32)
63 return sumSHAKE128(out, data, length)
64 }
65
66 func sumSHAKE128(out, data []byte, length int) []byte {
67 if len(out) < length {
68 out = make([]byte, length)
69 } else {
70 out = out[:length]
71 }
72 h := sha3.NewShake128()
73 h.Write(data)
74 h.Read(out)
75 return out
76 }
77
78
79
80 func SumSHAKE256(data []byte, length int) []byte {
81
82 out := make([]byte, 64)
83 return sumSHAKE256(out, data, length)
84 }
85
86 func sumSHAKE256(out, data []byte, length int) []byte {
87 if len(out) < length {
88 out = make([]byte, length)
89 } else {
90 out = out[:length]
91 }
92 h := sha3.NewShake256()
93 h.Write(data)
94 h.Read(out)
95 return out
96 }
97
98
99 type SHA3 struct {
100 s sha3.Digest
101 }
102
103
104 func New224() *SHA3 {
105 return &SHA3{*sha3.New224()}
106 }
107
108
109 func New256() *SHA3 {
110 return &SHA3{*sha3.New256()}
111 }
112
113
114 func New384() *SHA3 {
115 return &SHA3{*sha3.New384()}
116 }
117
118
119 func New512() *SHA3 {
120 return &SHA3{*sha3.New512()}
121 }
122
123
124 func (s *SHA3) Write(p []byte) (n int, err error) {
125 return s.s.Write(p)
126 }
127
128
129 func (s *SHA3) Sum(b []byte) []byte {
130 return s.s.Sum(b)
131 }
132
133
134 func (s *SHA3) Reset() {
135 s.s.Reset()
136 }
137
138
139 func (s *SHA3) Size() int {
140 return s.s.Size()
141 }
142
143
144 func (s *SHA3) BlockSize() int {
145 return s.s.BlockSize()
146 }
147
148
149 func (s *SHA3) MarshalBinary() ([]byte, error) {
150 return s.s.MarshalBinary()
151 }
152
153
154 func (s *SHA3) AppendBinary(p []byte) ([]byte, error) {
155 return s.s.AppendBinary(p)
156 }
157
158
159 func (s *SHA3) UnmarshalBinary(data []byte) error {
160 return s.s.UnmarshalBinary(data)
161 }
162
163
164 type SHAKE struct {
165 s sha3.SHAKE
166 }
167
168
169 func NewSHAKE128() *SHAKE {
170 return &SHAKE{*sha3.NewShake128()}
171 }
172
173
174 func NewSHAKE256() *SHAKE {
175 return &SHAKE{*sha3.NewShake256()}
176 }
177
178
179
180
181
182
183 func NewCSHAKE128(N, S []byte) *SHAKE {
184 return &SHAKE{*sha3.NewCShake128(N, S)}
185 }
186
187
188
189
190
191
192 func NewCSHAKE256(N, S []byte) *SHAKE {
193 return &SHAKE{*sha3.NewCShake256(N, S)}
194 }
195
196
197
198
199 func (s *SHAKE) Write(p []byte) (n int, err error) {
200 return s.s.Write(p)
201 }
202
203
204
205
206 func (s *SHAKE) Read(p []byte) (n int, err error) {
207 return s.s.Read(p)
208 }
209
210
211 func (s *SHAKE) Reset() {
212 s.s.Reset()
213 }
214
215
216 func (s *SHAKE) BlockSize() int {
217 return s.s.BlockSize()
218 }
219
220
221 func (s *SHAKE) MarshalBinary() ([]byte, error) {
222 return s.s.MarshalBinary()
223 }
224
225
226 func (s *SHAKE) AppendBinary(p []byte) ([]byte, error) {
227 return s.s.AppendBinary(p)
228 }
229
230
231 func (s *SHAKE) UnmarshalBinary(data []byte) error {
232 return s.s.UnmarshalBinary(data)
233 }
234
View as plain text