diff --git a/src/messages/userDataRequest.ts b/src/messages/userDataRequest.ts index 4465d08..b7d25d4 100644 --- a/src/messages/userDataRequest.ts +++ b/src/messages/userDataRequest.ts @@ -33,9 +33,11 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage, const packedData = new SmartBuffer() packedData.writeBytes(usernameLength) packedData.writeBytes(username) + packedData.pad(32 - username.length) packedData.writeBytes(colour) packedData.writeBytes(clientIdLength) packedData.writeBytes(clientId) + packedData.pad(32 - clientId.length) // Encrypt the data. const data = encrypt(packedData.data, MESSAGE_TYPE, key) diff --git a/src/messages/userDataResponse.ts b/src/messages/userDataResponse.ts index fe8ceba..efea90f 100644 --- a/src/messages/userDataResponse.ts +++ b/src/messages/userDataResponse.ts @@ -33,9 +33,11 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage const packedData = new SmartBuffer() packedData.writeBytes(usernameLength) packedData.writeBytes(username) + packedData.pad(32 - username.length) packedData.writeBytes(colour) packedData.writeBytes(clientIdLength) packedData.writeBytes(clientId) + packedData.pad(32 - clientId.length) // Return encrypted data. const data = encrypt(packedData.data, MESSAGE_TYPE, key) diff --git a/src/utilities/smart-buffer.ts b/src/utilities/smart-buffer.ts index c7dbea1..3c80031 100644 --- a/src/utilities/smart-buffer.ts +++ b/src/utilities/smart-buffer.ts @@ -79,7 +79,8 @@ export class SmartBuffer { * @param length The number of bytes to pad. */ pad (length: number): void { - this._data.push(...Array(length)) + this._data.push(...Array(length).fill(0)) + this.cursor += length } /** @@ -100,7 +101,7 @@ export class SmartBuffer { */ splice (start: number, deleteCount: number, ...items: number[]): void { if (this.length < start) { - this.pad(start) + this._data.push(...Array(start)) } this._data.splice(this.cursor, deleteCount, ...items) } diff --git a/tests/messages/userDataRequest.test.ts b/tests/messages/userDataRequest.test.ts index e7e2973..7b6b822 100644 --- a/tests/messages/userDataRequest.test.ts +++ b/tests/messages/userDataRequest.test.ts @@ -23,10 +23,10 @@ test('Create a user data request (0x0002) packet.', () => { // Then // We can't check the contents of the data as it's encrypted with a random nonce. // Check the message type and length. - expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x3A])) + expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x67])) // Check the total length is as expected. - expect(packedPacket.length).toBe(62) + expect(packedPacket.length).toBe(107) }) test('Parse a user data request (0x0002).', () => { diff --git a/tests/messages/userDataResponse.test.ts b/tests/messages/userDataResponse.test.ts index 99ca6f0..c957dd3 100644 --- a/tests/messages/userDataResponse.test.ts +++ b/tests/messages/userDataResponse.test.ts @@ -23,10 +23,10 @@ test('Create a user data response (0x0003) packet.', () => { // Then // We can't check the contents of the data as it's encrypted with a random nonce. // Check the message type and length. - expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x03, 0x00, 0x3A])) + expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x03, 0x00, 0x67])) // Check the total length is as expected. - expect(packedPacket.length).toBe(62) + expect(packedPacket.length).toBe(107) }) test('Parse a user data response (0x0003).', () => {