-
Notifications
You must be signed in to change notification settings - Fork 0
/
blake2b.go
90 lines (71 loc) · 1.87 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
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
package blake2b
import (
"hash"
"github.com/ThotaGopichandThota/gn-core3/hashing"
blake2bLib "golang.org/x/crypto/blake2b"
)
var _ hashing.Hasher = (*blake2b)(nil)
// blake2b is a blake2b implementation of the hasher interface.
type blake2b struct {
// customHashSize holds the custom value for the hash size. if not set, it will be 0 and the
// default hasher will be used
customHashSize int
emptyHash []byte
}
// NewBlake2bWithSize returns a new instance of the blake2b with a custom size
func NewBlake2bWithSize(hashSize int) (*blake2b, error) {
if hashSize < 0 {
return nil, ErrInvalidHashSize
}
h := &blake2b{
customHashSize: hashSize,
}
h.emptyHash = h.computeEmptyHash()
return h, nil
}
// NewBlake2b returns a new instance of the blake2b hasher
func NewBlake2b() *blake2b {
h := &blake2b{
customHashSize: 0,
}
h.emptyHash = h.computeEmptyHash()
return h
}
func (b2b *blake2b) getHasher() hash.Hash {
if b2b.customHashSize == 0 {
h, _ := blake2bLib.New256(nil)
return h
}
h, _ := blake2bLib.New(b2b.customHashSize, nil)
return h
}
// Compute takes a string, and returns the blake2b hash of that string
func (b2b *blake2b) Compute(s string) []byte {
if len(s) == 0 {
return b2b.getEmptyHash()
}
h := b2b.getHasher()
_, _ = h.Write([]byte(s))
return h.Sum(nil)
}
// Size returns the size, in number of bytes, of a blake2b hash
func (b2b *blake2b) Size() int {
if b2b.customHashSize == 0 {
return blake2bLib.Size256
}
return b2b.customHashSize
}
func (b2b *blake2b) getEmptyHash() []byte {
hashCopy := make([]byte, len(b2b.emptyHash))
copy(hashCopy, b2b.emptyHash)
return hashCopy
}
func (b2b *blake2b) computeEmptyHash() []byte {
h := b2b.getHasher()
_, _ = h.Write([]byte(""))
return h.Sum(nil)
}
// IsInterfaceNil returns true if there is no value under the interface
func (b2b *blake2b) IsInterfaceNil() bool {
return b2b == nil
}