forked from aliyun/aliyun-datahub-sdk-go
/
wrapper.go
48 lines (35 loc) · 1.07 KB
/
wrapper.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
package util
import (
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"reflect"
)
func WrapMessage(data []byte) []byte {
//data =[]byte("hello world!")
h1 := []byte("DHUB")
crc32c := crc32.MakeTable(crc32.Castagnoli)
crc := crc32.Checksum(data, crc32c)
h2 := make([]byte, 4)
binary.BigEndian.PutUint32(h2, crc)
h3 := make([]byte, 4)
binary.BigEndian.PutUint32(h3, uint32(len(data)))
//buf := make([]byte,0,len(h1)+len(h2)+len(h3)+len(data))
buf := append(h1, h2...)
buf = append(buf, h3...)
buf = append(buf, data...)
return buf
}
func UnwrapMessage(data []byte) ([]byte, error) {
crc := data[4:8]
body := data[12:]
crc32c := crc32.MakeTable(crc32.Castagnoli)
cs := crc32.Checksum(body, crc32c)
computedCrc := make([]byte, 4)
binary.BigEndian.PutUint32(computedCrc, cs)
if !reflect.DeepEqual(crc, computedCrc) {
return nil, errors.New(fmt.Sprintf("Parse pb response body fail, error: crc check error. crc: %s, compute crc: %s", crc, computedCrc))
}
return body, nil
}