This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
flaghasher.go
76 lines (62 loc) · 1.55 KB
/
flaghasher.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
package lazyledger
import (
"hash"
)
type flagDigest struct {
flagger Flagger
baseHasher hash.Hash
data []byte
codedMode bool
}
// NewFlagHasher returns a new hash.Hash computing checksums using the bashHasher with flags from flagger.
func NewFlagHasher(flagger Flagger, baseHasher hash.Hash) hash.Hash {
return &flagDigest{
flagger: flagger,
baseHasher: baseHasher,
}
}
func (d *flagDigest) setCodedMode(mode bool) {
d.codedMode = mode
}
func (d *flagDigest) Write(p []byte) (int, error) {
d.data = append(d.data, p...)
return d.baseHasher.Write(p)
}
func (d *flagDigest) Sum(in []byte) []byte {
in = append(in, d.parentFlag()...)
return d.baseHasher.Sum(in)
}
func (d *flagDigest) Size() int {
return d.flagger.FlagSize() + d.baseHasher.Size()
}
func (d *flagDigest) BlockSize() int {
return d.baseHasher.BlockSize()
}
func (d *flagDigest) Reset() {
d.data = nil
d.baseHasher.Reset()
}
func (d *flagDigest) leftFlag() []byte {
return d.flagger.NodeFlag(d.data[1:d.Size()+1])
}
func (d *flagDigest) rightFlag() []byte {
return d.flagger.NodeFlag(d.data[1+d.Size():])
}
func (d *flagDigest) parentFlag() []byte {
if d.isLeaf() {
if d.codedMode {
return codedFlag[:]
}
return d.flagger.LeafFlag(d.mainData())
}
return d.flagger.Union(d.leftFlag(), d.rightFlag())
}
func (d *flagDigest) mainData() []byte {
return d.data[1:]
}
func (d *flagDigest) isLeaf() bool {
if d.data[0] == byte(0) {
return true
}
return false
}