-
Notifications
You must be signed in to change notification settings - Fork 199
/
blake2b.go
65 lines (53 loc) · 1.26 KB
/
blake2b.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
package blake2b
import (
"hash"
"github.com/ElrondNetwork/elrond-go/hashing"
"golang.org/x/crypto/blake2b"
)
var _ hashing.Hasher = (*Blake2b)(nil)
// Blake2b is a blake2b implementation of the hasher interface.
type Blake2b struct {
HashSize int
emptyHash []byte
}
func (b2b *Blake2b) getHasher() hash.Hash {
if b2b.HashSize == 0 {
h, _ := blake2b.New256(nil)
return h
}
h, _ := blake2b.New(b2b.HashSize, nil)
return h
}
func (b2b *Blake2b) setEmpty() {
b2b.emptyHash = b2b.getHasher().Sum(nil)
}
// Compute takes a string, and returns the blake2b hash of that string
func (b2b *Blake2b) Compute(s string) []byte {
if len(s) == 0 {
if len(b2b.emptyHash) == 0 {
b2b.setEmpty()
}
return b2b.emptyHash
}
h := b2b.getHasher()
_, _ = h.Write([]byte(s))
return h.Sum(nil)
}
// EmptyHash returns the blake2b hash of the empty string
func (b2b *Blake2b) EmptyHash() []byte {
if len(b2b.emptyHash) == 0 {
b2b.setEmpty()
}
return b2b.emptyHash
}
// Size returns the size, in number of bytes, of a blake2b hash
func (b2b *Blake2b) Size() int {
if b2b.HashSize == 0 {
return blake2b.Size256
}
return b2b.HashSize
}
// IsInterfaceNil returns true if there is no value under the interface
func (b2b *Blake2b) IsInterfaceNil() bool {
return b2b == nil
}