mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-02-05 09:42:56 +01:00
Progress with binary8 encoding
This commit is contained in:
@@ -69,10 +69,40 @@ func (cc *CryptConn) ReadPacket() ([]byte, error) {
|
||||
}
|
||||
|
||||
_ = combinedCheck
|
||||
/*
|
||||
fmt.Printf("cc %X, c0 %X, c1 %X, c2 %X\n", combinedCheck, check0, check1, check2)
|
||||
fmt.Printf("cc %X, c0 %X, c1 %X, c2 %X\n", cph.PrevPacketCombinedCheck, cph.Check0, cph.Check1, cph.Check2)
|
||||
fmt.Printf("cph: %+v\n", cph)
|
||||
*/
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// SendPacket encrypts and sends a packet.
|
||||
func (cc *CryptConn) SendPacket(data []byte) error {
|
||||
keyRotDelta := byte(3)
|
||||
|
||||
if keyRotDelta != 0 {
|
||||
cc.sendKeyRot = (uint32(keyRotDelta) * (cc.sendKeyRot + 1))
|
||||
}
|
||||
|
||||
// Encrypt the data
|
||||
encData, combinedCheck, check0, check1, check2 := crypto.Encrypt(data, cc.sendKeyRot, nil)
|
||||
|
||||
header := &CryptPacketHeader{}
|
||||
header.Pf0 = byte(((uint(len(encData)) >> 12) & 0xF3) | 3)
|
||||
header.KeyRotDelta = keyRotDelta
|
||||
header.PacketNum = uint16(cc.sentPackets)
|
||||
header.DataSize = uint16(len(encData))
|
||||
header.PrevPacketCombinedCheck = cc.prevSendPacketCombinedCheck
|
||||
header.Check0 = check0
|
||||
header.Check1 = check1
|
||||
header.Check2 = check2
|
||||
|
||||
headerBytes, err := header.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cc.conn.Write(headerBytes)
|
||||
cc.conn.Write(encData)
|
||||
|
||||
cc.sentPackets++
|
||||
cc.prevSendPacketCombinedCheck = combinedCheck
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -65,3 +65,26 @@ func NewCryptPacketHeader(data []byte) (*CryptPacketHeader, error) {
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// Encode encodes the CryptPacketHeader into raw bytes.
|
||||
func (c *CryptPacketHeader) Encode() ([]byte, error) {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
var data = []interface{}{
|
||||
c.Pf0,
|
||||
c.KeyRotDelta,
|
||||
c.PacketNum,
|
||||
c.DataSize,
|
||||
c.PrevPacketCombinedCheck,
|
||||
c.Check0,
|
||||
c.Check1,
|
||||
c.Check2,
|
||||
}
|
||||
for _, v := range data {
|
||||
err := binary.Write(buf, binary.BigEndian, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user