-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBaseHashCoder.go
59 lines (45 loc) · 934 Bytes
/
BaseHashCoder.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
package main
import (
"encoding/hex"
"fmt"
"hash"
"sync"
)
type BaseHashCoder struct {
datas chan []byte
Encoder hash.Hash
}
func (_self *BaseHashCoder) Name() string {
return "-"
}
func (_self *BaseHashCoder) Create() {
}
func (_self *BaseHashCoder) ReadFromString(input string) {
_self.ReadFromBytes([]byte(input))
}
func (_self *BaseHashCoder) ReadFromBytes(input []byte) {
_self.Encoder.Write(input)
}
func (_self *BaseHashCoder) ReadFromChan(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
for data := range _self.datas {
_self.Encoder.Write(data)
}
wg.Done()
}()
}
func (_self *BaseHashCoder) WriteToChan(input []byte) {
_self.datas <- input
}
func (_self *BaseHashCoder) CloseChan() {
close(_self.datas)
}
func (_self *BaseHashCoder) GenHashHex() string {
if _self.Encoder == nil {
fmt.Errorf("no encoder")
return ""
}
hash := hex.EncodeToString(_self.Encoder.Sum(nil))
return hash
}