-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash256.go
45 lines (36 loc) · 848 Bytes
/
hash256.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
// TODO: copyright
// Delete this file if we implement a Digest() function for all necessary types
package crypto
import (
"crypto/sha256"
"fmt"
"hash"
)
type Hasher256 struct {
hasher hash.Hash
}
func NewHasher256() *Hasher256 {
return &Hasher256{sha256.New()}
}
func (hasher *Hasher256) Feed(data []byte) *Hasher256 {
_, err := hasher.hasher.Write(data)
if err != nil {
panic(fmt.Errorf("hash256: Feed encounters unexpected error %v", err))
}
return hasher
}
func (hasher *Hasher256) Sum(data []byte) *Digest256 {
sum := hasher.hasher.Sum(data)
return &Digest256{sum[:]}
}
func (hasher *Hasher256) Size() int {
return hasher.hasher.Size()
}
func (hasher *Hasher256) Reset() *Hasher256 {
hasher.hasher.Reset()
return hasher
}
func Hash256(data []byte) *Digest256 {
sum := sha256.Sum256(data)
return &Digest256{sum[:]}
}