Skip to content

Commit

Permalink
optimize(utils): hash & image_resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed May 6, 2024
1 parent 5496c98 commit e0822da
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 123 deletions.
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (c *QQClient) FecthQrcode() ([]byte, string, error) {
WriteU8(0).
WriteTlv([][]byte{
tlv.T16(c.appInfo.AppID, c.appInfo.SubAppID,
utils.GetBytesFromHex(c.deviceInfo.Guid), c.appInfo.PTVersion, c.appInfo.PackageName),
utils.MustParseHexStr(c.deviceInfo.Guid), c.appInfo.PTVersion, c.appInfo.PackageName),
tlv.T1b(),
tlv.T1d(c.appInfo.MiscBitmap),
tlv.T33(utils.GetBytesFromHex(c.deviceInfo.Guid)),
tlv.T33(utils.MustParseHexStr(c.deviceInfo.Guid)),
tlv.T35(c.appInfo.PTOSVersion),
tlv.T66(c.appInfo.PTOSVersion),
tlv.Td1(c.appInfo.OS, c.deviceInfo.DeviceName),
Expand Down Expand Up @@ -172,7 +172,7 @@ func (c *QQClient) KeyExchange() {
}

func (c *QQClient) PasswordLogin(password string) (loginState.State, error) {
md5Password := utils.Md5Digest([]byte(password))
md5Password := utils.MD5Digest([]byte(password))

cr := tlv.T106(
c.appInfo.AppID,
Expand Down Expand Up @@ -233,7 +233,7 @@ func (c *QQClient) QrcodeLogin(refreshInterval int) (bool, error) {
tlv.T144(c.sig.Tgtgt, app, device),
tlv.T116(app.SubSigmap),
tlv.T142(app.PackageName, 0),
tlv.T145(utils.GetBytesFromHex(device.Guid)),
tlv.T145(utils.MustParseHexStr(device.Guid)),
tlv.T18(0, app.AppClientVersion, int(c.Uin), 0, 5, 0),
tlv.T141([]byte("Unknown"), make([]byte, 0)),
tlv.T177(app.WTLoginSDK, 0),
Expand Down
2 changes: 1 addition & 1 deletion client/highway.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *QQClient) SendUpBlockAsync(block UpBlock, server string) bool {
DataFlag: 16,
CommandId: uint32(block.CommandId),
}
md5 := utils.Md5Digest(block.Block)
md5 := utils.MD5Digest(block.Block)
segHead := &highway.SegHead{
ServiceId: proto.Some(uint32(0)),
Filesize: block.FileSize,
Expand Down
2 changes: 1 addition & 1 deletion client/ntlogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func buildNtloginRequest(uin uint32, app *info.AppInfo, device *info.DeviceInfo,
1: app.OS,
2: device.DeviceName,
3: app.NTLoginType,
4: utils.GetBytesFromHex(device.Guid),
4: utils.MustParseHexStr(device.Guid),
},
3: proto.DynamicMessage{
1: device.KernelVersion,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.19

require (
github.com/RomiChan/protobuf v0.1.1-0.20230204044148-2ed269a2e54d
github.com/fumiama/imgsz v0.0.4
github.com/mattn/go-colorable v0.1.13
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
golang.org/x/image v0.16.0 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c/go.mod h1:KqZzu7slN
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fumiama/imgsz v0.0.4 h1:Lsasu2hdSSFS+vnD+nvR1UkiRMK7hcpyYCC0FzgSMFI=
github.com/fumiama/imgsz v0.0.4/go.mod h1:bISOQVTlw9sRytPwe8ir7tAaEmyz9hSNj9n8mXMBG0E=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
Expand All @@ -16,11 +18,11 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/image v0.16.0 h1:9kloLAKhUufZhA12l5fwnx2NZW39/we1UhBesW433jw=
golang.org/x/image v0.16.0/go.mod h1:ugSZItdV4nOxyqp56HmXwH0Ry0nBCpjnZdpDaIHdoPs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions info/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type DeviceInfo struct {

func NewDeviceInfo(uin int) *DeviceInfo {
return &DeviceInfo{
Guid: fmt.Sprintf("%X", utils.Md5Digest([]byte(strconv.Itoa(uin)))),
DeviceName: fmt.Sprintf("Lagrange-%X", utils.Md5Digest([]byte(strconv.Itoa(uin)))[0:4]),
Guid: fmt.Sprintf("%X", utils.MD5Digest([]byte(strconv.Itoa(uin)))),
DeviceName: fmt.Sprintf("Lagrange-%X", utils.MD5Digest([]byte(strconv.Itoa(uin)))[0:4]),
SystemKernel: fmt.Sprintf("%s %s", platform.GetSystem(), platform.GetVersion()),
KernelVersion: platform.GetVersion(),
}
Expand Down
4 changes: 2 additions & 2 deletions info/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Encode(sig *SigInfo) []byte {
if err != nil {
panic(err)
}
dataHash := utils.Md5Digest(buffer.Bytes())
dataHash := utils.MD5Digest(buffer.Bytes())

return binary.NewBuilder(nil).
WriteBytes(dataHash, true).
Expand All @@ -35,7 +35,7 @@ func Decode(buf []byte, verify bool) *SigInfo {
dataHash := reader.ReadBytesWithLength("u16", false)
data := reader.ReadBytesWithLength("u16", false)

if verify && string(dataHash) != string(utils.Md5Digest(data)) {
if verify && string(dataHash) != string(utils.MD5Digest(data)) {
panic("Data hash does not match")
}
buffer := bytes.NewBuffer(data)
Expand Down
13 changes: 7 additions & 6 deletions packets/oidb/GroupImageUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"encoding/hex"
"errors"
"fmt"
"math/rand"

"github.com/LagrangeDev/LagrangeGo/packets/pb/service/oidb"
"github.com/LagrangeDev/LagrangeGo/utils"
"math/rand"
)

func BuildGroupImageUploadReq(groupUin uint32, data []byte) (*OidbPacket, error) {
// OidbSvcTrpcTcp.0x11c4_100
md5Hash := utils.Md5Digest(data)
sha1Hash := utils.Sha1Digest(data)
md5Hash := utils.MD5Digest(data)
sha1Hash := utils.SHA1Digest(data)
format, size, err := utils.ImageResolve(data)
if err != nil {
return nil, err
}
imageExt := utils.GetImageExt(format)
imageExt := format.String()

hexString := "0800180020004a00500062009201009a0100aa010c080012001800200028003a00"
bytesPbReserveTroop, err := hex.DecodeString(hexString)
Expand Down Expand Up @@ -57,8 +58,8 @@ func BuildGroupImageUploadReq(groupUin uint32, data []byte) (*OidbPacket, error)
VideoFormat: 0,
VoiceFormat: 0,
},
Width: size.X,
Height: size.Y,
Width: uint32(size.Width),
Height: uint32(size.Height),
Time: 0,
Original: 1,
},
Expand Down
13 changes: 7 additions & 6 deletions packets/oidb/PrivateImageUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"encoding/hex"
"errors"
"fmt"
"math/rand"

"github.com/LagrangeDev/LagrangeGo/packets/pb/service/oidb"
"github.com/LagrangeDev/LagrangeGo/utils"
"math/rand"
)

func BuildImageUploadReq(targetUid string, data []byte) (*OidbPacket, error) {
// OidbSvcTrpcTcp.0x11c5_100
md5Hash := utils.Md5Digest(data)
sha1Hash := utils.Sha1Digest(data)
md5Hash := utils.MD5Digest(data)
sha1Hash := utils.SHA1Digest(data)
format, size, err := utils.ImageResolve(data)
if err != nil {
return nil, err
}
imageExt := utils.GetImageExt(format)
imageExt := format.String()

hexString := "0800180020004200500062009201009a0100a2010c080012001800200028003a00"
bytesPbReserveC2c, err := hex.DecodeString(hexString)
Expand Down Expand Up @@ -58,8 +59,8 @@ func BuildImageUploadReq(targetUid string, data []byte) (*OidbPacket, error) {
VideoFormat: 0,
VoiceFormat: 0,
},
Width: size.X,
Height: size.Y,
Width: uint32(size.Width),
Height: uint32(size.Height),
Time: 0,
Original: 1,
},
Expand Down
6 changes: 3 additions & 3 deletions packets/tlv/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func T100(ssoVersion, appID, subAppID, appClientVersion, sigmap, dbBufVer int) [
// T106 抄的时候注意参数顺序
func T106(appId, appClientVersion, uin int, guid string, passwordMd5, tgtgtKey, ip []byte, savePassword bool) []byte {
// password_md5 + bytes(4) + write_u32(uin).pack()
key := utils.Md5Digest(append(passwordMd5, append(make([]byte, 4),
key := utils.MD5Digest(append(passwordMd5, append(make([]byte, 4),
utils.NewPacketBuilder(nil).WriteU32(uint32(uin)).Pack(-1)...)...))

body := utils.NewPacketBuilder(nil).
Expand All @@ -53,7 +53,7 @@ func T106(appId, appClientVersion, uin int, guid string, passwordMd5, tgtgtKey,
WriteBytes(tgtgtKey, "", true).
WriteU32(0).
WriteBool(true).
WriteBytes(utils.GetBytesFromHex(guid), "", true).
WriteBytes(utils.MustParseHexStr(guid), "", true).
WriteU32(0).
WriteU32(1).
WriteString(strconv.Itoa(uin), "u16", false).
Expand Down Expand Up @@ -125,7 +125,7 @@ func T144(tgtgtKey []byte, appInfo *info.AppInfo, device *info.DeviceInfo) []byt
WriteTlv([][]byte{
T16e(device.DeviceName),
T147(appInfo.AppID, appInfo.PTVersion, appInfo.PackageName),
T128(appInfo.OS, utils.GetBytesFromHex(device.Guid)),
T128(appInfo.OS, utils.MustParseHexStr(device.Guid)),
T124(),
}).Pack(0x144)
}
Expand Down
3 changes: 2 additions & 1 deletion packets/wtlogin/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wtlogin

import (
"encoding/hex"

"github.com/LagrangeDev/LagrangeGo/packets/pb/login"

"github.com/LagrangeDev/LagrangeGo/info"
Expand Down Expand Up @@ -31,7 +32,7 @@ func BuildKexExchangeRequest(uin uint32, guid string) []byte {
WriteU32(uint32(utils.TimeStamp())).
Pack(-1)

p2Hash := utils.Sha256Digest(p2)
p2Hash := utils.SHA256Digest(p2)
encP2Hash := crypto.AesGCMEncrypt(p2Hash, encKey)

return proto.DynamicMessage{
Expand Down
10 changes: 5 additions & 5 deletions packets/wtlogin/oicq.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func BuildUniPacket(uin, seq int, cmd string, sign map[string]string,

if sign != nil {
head[24] = proto.DynamicMessage{
1: utils.GetBytesFromHex(sign["sign"]),
2: utils.GetBytesFromHex(sign["token"]),
3: utils.GetBytesFromHex(sign["extra"]),
1: utils.MustParseHexStr(sign["sign"]),
2: utils.MustParseHexStr(sign["token"]),
3: utils.MustParseHexStr(sign["extra"]),
}
}

Expand All @@ -112,7 +112,7 @@ func BuildUniPacket(uin, seq int, cmd string, sign map[string]string,
WriteBytes(sigInfo.Tgt, "u32", true).
WriteString(cmd, "u32", true).
WriteBytes(make([]byte, 0), "u32", true).
WriteBytes(utils.GetBytesFromHex(deviceInfo.Guid), "u32", true).
WriteBytes(utils.MustParseHexStr(deviceInfo.Guid), "u32", true).
WriteBytes(make([]byte, 0), "u32", true).
WriteString(appInfo.CurrentVersion, "u16", true).
WriteBytes(head.Encode(), "u32", true).
Expand Down Expand Up @@ -172,7 +172,7 @@ func DecodeLoginResponse(buf []byte, sig *info.SigInfo) (bool, error) {
sig.Gender = tlvReader.ReadU8()
sig.Nickname = tlvReader.ReadStringWithLength("u8", false)
}
sig.Tgtgt = utils.Md5Digest(sig.D2Key)
sig.Tgtgt = utils.MD5Digest(sig.D2Key)
sig.TempPwd = tlv[0x106]

var resp pb.Tlv543
Expand Down
24 changes: 12 additions & 12 deletions utils/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"crypto/sha256"
)

func Md5Digest(v []byte) []byte {
hasher := md5.New()
hasher.Write(v)
return hasher.Sum(nil)
func MD5Digest(v []byte) []byte {
h := md5.New()
h.Write(v)
return h.Sum(make([]byte, 0, md5.Size))
}

func Sha256Digest(v []byte) []byte {
sha256er := sha256.New()
sha256er.Write(v)
return sha256er.Sum(nil)
func SHA256Digest(v []byte) []byte {
h := sha256.New()
h.Write(v)
return h.Sum(make([]byte, 0, sha256.Size))
}

func Sha1Digest(v []byte) []byte {
sha1er := sha1.New()
sha1er.Write(v)
return sha1er.Sum(nil)
func SHA1Digest(v []byte) []byte {
h := sha1.New()
h.Write(v)
return h.Sum(make([]byte, 0, sha1.Size))
}
Loading

0 comments on commit e0822da

Please sign in to comment.