forked from LagrangeDev/LagrangeGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sig.go
68 lines (57 loc) · 1.33 KB
/
sig.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
package auth
import (
"bytes"
"encoding/gob"
"errors"
"github.com/2mf8/LagrangeGo/utils/binary"
"github.com/2mf8/LagrangeGo/utils/crypto"
)
var (
ErrDataHashMismatch = errors.New("data hash mismatch")
)
type SigInfo struct {
Uin uint32
Sequence uint32
Uid string
Tgtgt []byte
Tgt []byte
D2 []byte
D2Key []byte
Qrsig []byte
ExchangeKey []byte
KeySig []byte
Cookies string
UnusualSig []byte
TempPwd []byte
CaptchaInfo [3]string
Nickname string
Age uint8
Gender uint8
}
func init() {
// 这里不注册好像也可以
gob.Register(SigInfo{})
}
func (sig *SigInfo) Marshal() ([]byte, error) {
buffer := new(bytes.Buffer)
err := gob.NewEncoder(buffer).Encode(sig)
if err != nil {
return nil, err
}
dataHash := crypto.MD5Digest(buffer.Bytes())
return binary.NewBuilder(nil).
WriteLenBytes(dataHash).
WriteLenBytes(buffer.Bytes()).
ToBytes(), nil
}
func UnmarshalSigInfo(buf []byte, verify bool) (siginfo SigInfo, err error) {
reader := binary.NewReader(buf)
dataHash := reader.ReadBytesWithLength("u16", false)
data := reader.ReadBytesWithLength("u16", false)
if verify && !bytes.Equal(dataHash, crypto.MD5Digest(data)) {
err = ErrDataHashMismatch
return
}
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&siginfo)
return
}