github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/auth/sig.go (about) 1 package auth 2 3 import ( 4 "bytes" 5 "encoding/gob" 6 "errors" 7 8 "github.com/LagrangeDev/LagrangeGo/utils/binary" 9 "github.com/LagrangeDev/LagrangeGo/utils/crypto" 10 ) 11 12 var ( 13 ErrDataHashMismatch = errors.New("data hash mismatch") 14 ) 15 16 type SigInfo struct { 17 Uin uint32 18 Sequence uint32 19 Uid string 20 Tgtgt []byte 21 Tgt []byte 22 D2 []byte 23 D2Key []byte 24 Qrsig []byte 25 ExchangeKey []byte 26 KeySig []byte 27 Cookies string 28 UnusualSig []byte 29 TempPwd []byte 30 CaptchaInfo [3]string 31 32 Nickname string 33 Age uint8 34 Gender uint8 35 } 36 37 func init() { 38 // 这里不注册好像也可以 39 gob.Register(SigInfo{}) 40 } 41 42 func (sig *SigInfo) Marshal() ([]byte, error) { 43 buffer := new(bytes.Buffer) 44 err := gob.NewEncoder(buffer).Encode(sig) 45 if err != nil { 46 return nil, err 47 } 48 dataHash := crypto.MD5Digest(buffer.Bytes()) 49 50 return binary.NewBuilder(nil). 51 WriteLenBytes(dataHash). 52 WriteLenBytes(buffer.Bytes()). 53 ToBytes(), nil 54 } 55 56 func UnmarshalSigInfo(buf []byte, verify bool) (siginfo SigInfo, err error) { 57 reader := binary.NewReader(buf) 58 dataHash := reader.ReadBytesWithLength("u16", false) 59 data := reader.ReadBytesWithLength("u16", false) 60 61 if verify && !bytes.Equal(dataHash, crypto.MD5Digest(data)) { 62 err = ErrDataHashMismatch 63 return 64 } 65 66 err = gob.NewDecoder(bytes.NewReader(data)).Decode(&siginfo) 67 return 68 }