-
Notifications
You must be signed in to change notification settings - Fork 408
/
md5.go
76 lines (63 loc) · 1.29 KB
/
md5.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 md5 provides a integrity check transfer filter
package md5
import (
"bytes"
"crypto/md5"
"errors"
"github.com/andeya/erpc/v7/xfer"
)
// Reg registers a md5 checker filter for transfer.
func Reg(id byte, name string) {
xfer.Reg(&md5Hash{
id: id,
name: name,
})
}
// md5Hash compression filter
type md5Hash struct {
id byte
name string
}
const md5Length = 16
var errDataCheck = errors.New("check failed")
// ID returns transfer filter id.
func (m *md5Hash) ID() byte {
return m.id
}
// ID returns transfer filter name.
func (m *md5Hash) Name() string {
return m.name
}
func (m *md5Hash) OnPack(src []byte) ([]byte, error) {
content, err := getMd5(src)
if err != nil {
return nil, err
}
src = append(src, content...)
return src, nil
}
func (m *md5Hash) OnUnpack(src []byte) ([]byte, error) {
srcLength := len(src)
if srcLength < md5Length {
return nil, errDataCheck
}
srcData := src[:srcLength-md5Length]
content, err := getMd5(srcData)
if err != nil {
return nil, err
}
// Check
if !bytes.Equal(content, src[srcLength-md5Length:]) {
return nil, errDataCheck
}
return srcData, nil
}
// Get md5 data
func getMd5(src []byte) ([]byte, error) {
newMd5 := md5.New()
_, err := newMd5.Write(src)
if err != nil {
return nil, err
}
return newMd5.Sum(nil), nil
}