-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum_reader.go
90 lines (66 loc) · 1.69 KB
/
checksum_reader.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 eyego
import (
"io"
"encoding/hex"
"fmt"
"crypto/md5"
"encoding/base64"
"bytes"
// "io/ioutil"
)
type ChecksumReader struct {
delegate io.Reader
state *checksumState
}
type checksumState struct {
checksums []uint16
}
func NewChecksumReader(r io.Reader) ChecksumReader {
cr := ChecksumReader {
state:&checksumState{
checksums: make([]uint16,0, 16)}}
bnr := NewBlockNotifyingReader(r, 512, func(b []byte) {
cr.appendBlock(b)
})
cr.delegate = bnr
return cr
}
func (r ChecksumReader) Read(p []byte) (n int, err error){
return r.delegate.Read(p)
}
func (cr ChecksumReader) Checksum(uploadKey string) string {
b, _ := hex.DecodeString(uploadKey)
if len(b)%2 != 0 { panic("Bad upload key")}
h := md5.New()
Trace("Beginning checksum")
for i := 0; i < len(cr.state.checksums); i++ {
Trace("Adding %v to hash", cr.state.checksums[i])
h.Write([]byte{
byte(cr.state.checksums[i]),
byte(cr.state.checksums[i]>>8)})
}
Trace("Adding %v to hash", b)
h.Write(b)
return fmt.Sprintf("%x", h.Sum(nil))
}
func (cr *ChecksumReader) appendBlock(b []byte) {
cr.state.checksums = append(cr.state.checksums, tcp_checksum(b))
}
func tcp_checksum(b []byte) uint16 {
if len(b) %2 != 0 { panic(fmt.Sprintf("tcp checksum bad length: %d", len(b))) }
buf := bytes.NewBuffer(make([]byte, 0))
enc := base64.NewEncoder(base64.StdEncoding, buf)
enc.Write(b)
// b64, _ := ioutil.ReadAll(buf)
// Trace("To Checksum: %v", string(b64))
var sum uint32 = 0
var tmp uint16
for c := 0; c < len(b); c = c + 2 {
tmp = uint16(b[c]) | uint16(b[c + 1])<<8
sum += uint32(tmp)
}
sum = (sum>>16) + (sum & 0xffff)
sum += (sum>>16)
Trace("tcp checksum: %d", uint16(^sum))
return uint16(^sum)
}