Return ciphertext rather than decrypted content
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
5c17b880cf
commit
be39f7bae1
File diff suppressed because it is too large
Load Diff
|
@ -20,11 +20,8 @@
|
|||
"@types/jest": "^27.4.0",
|
||||
"jest": "^27.4.7",
|
||||
"ts-jest": "^27.1.3",
|
||||
"ts-loader": "^9.2.6",
|
||||
"ts-standard": "^11.0.0",
|
||||
"typescript": "^4.5.5",
|
||||
"webpack": "^5.68.0",
|
||||
"webpack-cli": "^4.9.2"
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
"jest": {
|
||||
"verbose": true,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { decrypt, encrypt } from 'romulus-js'
|
||||
import { encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
@ -6,19 +6,16 @@ import { packOutgoingPacket } from './packet'
|
|||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Basic)
|
||||
|
||||
export interface BasicMessage {
|
||||
message: string
|
||||
success?: boolean
|
||||
data: Uint8Array
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing basic message (0x0001) packet.
|
||||
* @param properties The properties for the message.
|
||||
* @param message The plaintext message to send.
|
||||
* @param key The key to encrypt the data with.
|
||||
* @returns An outgoing basic message (0x0001) packet.
|
||||
* @returns An encrypted outgoing basic message (0x0001) packet.
|
||||
*/
|
||||
export function packBasicMessage (properties: BasicMessage, key: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const encoder = new TextEncoder()
|
||||
const message = encoder.encode(properties.message)
|
||||
export function packBasicMessage (message: Uint8Array, key: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const data = encrypt(message, MESSAGE_TYPE, key)
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
|
@ -29,14 +26,8 @@ export function packBasicMessage (properties: BasicMessage, key: Uint8Array = DE
|
|||
/**
|
||||
* Unpack the data section of an incoming basic message (0x0001) message.
|
||||
* @param data The data section of an incoming basic message (0x0001) message.
|
||||
* @param key The key to decrypt the data with.
|
||||
* @returns An unpacked basic message (0x0001) message.
|
||||
* @returns An encrypted unpacked basic message (0x0001) message.
|
||||
*/
|
||||
export function unpackBasicMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): BasicMessage {
|
||||
const decoder = new TextDecoder()
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
return {
|
||||
message: decoder.decode(message.plaintext),
|
||||
success: message.success
|
||||
}
|
||||
export function unpackBasicMessage (data: Uint8Array): Uint8Array {
|
||||
return data
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ export interface UserDataRequestMessage {
|
|||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
success?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,27 +47,13 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
|
|||
}
|
||||
|
||||
/**
|
||||
* Unpack the data section of an incoming user data request (0x0002) message
|
||||
* @param data The data section of an incoming user data request (0x0002) message
|
||||
* @param key The key to decrypt the data with.
|
||||
* @returns An unpacked user data request (0x0002) message
|
||||
* Unpack the decrypted data section of an incoming user data request (0x0002) message.
|
||||
* @param data The decrypted data section of an incoming user data request (0x0002) message.
|
||||
* @returns An unpacked user data request (0x0002) message.
|
||||
*/
|
||||
export function unpackUserDataRequestMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): UserDataRequestMessage {
|
||||
// Decrypt the incoming data.
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
// Guard to check if decryption was successful.
|
||||
if (!message.success) {
|
||||
return {
|
||||
username: '',
|
||||
colour: Color('black'),
|
||||
clientId: '',
|
||||
success: false
|
||||
}
|
||||
}
|
||||
|
||||
export function unpackUserDataRequestMessage (data: Uint8Array): UserDataRequestMessage {
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(message.plaintext)
|
||||
const packedData = SmartBuffer.from(data)
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
|
@ -83,6 +68,5 @@ export function unpackUserDataRequestMessage (data: Uint8Array, key: Uint8Array
|
|||
username: decoder.decode(username),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: decoder.decode(clientId),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ export interface UserDataResponseMessage {
|
|||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
success?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,27 +46,13 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
|
|||
}
|
||||
|
||||
/**
|
||||
* Unpack the data section of an incoming user data response (0x0003) message
|
||||
* @param data The data section of an incoming user data response (0x0003) message
|
||||
* @param key The key to decrypt the data with.
|
||||
* @returns An unpacked user data response (0x0003) message
|
||||
* Unpack the decrypted data section of an incoming user data response (0x0003) message.
|
||||
* @param data The decrypted data section of an incoming user data response (0x0003) message.
|
||||
* @returns A unpacked user data response (0x0003) message.
|
||||
*/
|
||||
export function unpackUserDataResponseMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): UserDataResponseMessage {
|
||||
// Decrypt the incoming data.
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
// Guard to check if decryption was successful.
|
||||
if (!message.success) {
|
||||
return {
|
||||
username: '',
|
||||
colour: Color('black'),
|
||||
clientId: '',
|
||||
success: false
|
||||
}
|
||||
}
|
||||
|
||||
export function unpackUserDataResponseMessage (data: Uint8Array): UserDataResponseMessage {
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(message.plaintext)
|
||||
const packedData = SmartBuffer.from(data)
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
|
@ -82,6 +67,5 @@ export function unpackUserDataResponseMessage (data: Uint8Array, key: Uint8Array
|
|||
username: decoder.decode(username),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: decoder.decode(clientId),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
import { MessageTypes } from '../../src/common'
|
||||
import { packers, unpackers } from '../../src/mapping'
|
||||
|
||||
const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F])
|
||||
const KEY = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
||||
|
||||
test('Create a basic message (0x0001) packet.', () => {
|
||||
// Given
|
||||
const message = 'Hello, World!'
|
||||
const encoder = new TextEncoder()
|
||||
const message = encoder.encode('Hello, World!')
|
||||
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.Basic](
|
||||
{ message: message },
|
||||
message,
|
||||
KEY
|
||||
)
|
||||
|
||||
|
@ -24,16 +25,12 @@ test('Create a basic message (0x0001) packet.', () => {
|
|||
|
||||
test('Parse a basic message (0x0001).', () => {
|
||||
// Given
|
||||
const ciphertext = new Uint8Array([
|
||||
0x84, 0xa2, 0x3a, 0xe2, 0xa8, 0xff, 0x43, 0x56, 0x96, 0x94, 0xf6, 0xe5, 0x1a, 0x30, 0x53,
|
||||
0x39, 0xb9, 0xc4, 0x2c, 0xab, 0x21, 0x52, 0x8f, 0xc2, 0xba, 0x6c, 0x8b, 0x96, 0x82, 0x9d,
|
||||
0x51, 0x27, 0x0c, 0xb6, 0x3b, 0x7f, 0x3e, 0x2a, 0x7b, 0x70, 0xbe, 0x01, 0x7b, 0x71, 0x9d
|
||||
])
|
||||
const data = new Uint8Array([1, 2, 3, 4])
|
||||
|
||||
// When
|
||||
const unpackedPacket = unpackers[MessageTypes.Basic](ciphertext, KEY)
|
||||
const unpackedPacket = unpackers[MessageTypes.Basic](data)
|
||||
|
||||
// Then
|
||||
expect(unpackedPacket.success).toBe(true)
|
||||
expect(unpackedPacket.message).toBe('Hello, World!')
|
||||
expect(unpackedPacket)
|
||||
expect(unpackedPacket).toMatchObject(data)
|
||||
})
|
||||
|
|
|
@ -31,22 +31,15 @@ test('Create a user data request (0x0002) packet.', () => {
|
|||
|
||||
test('Parse a user data request (0x0002).', () => {
|
||||
// Given
|
||||
const ciphertext = new Uint8Array([
|
||||
0x6e, 0x3f, 0xe8, 0x45, 0x65, 0x59, 0x45, 0x85, 0xb4, 0xb2, 0xbc, 0x7a, 0x03, 0xc5,
|
||||
0x6d, 0xf4, 0x23, 0x3e, 0xe9, 0x3b, 0x08, 0x6d, 0x67, 0x85, 0xc1, 0xda, 0xc6, 0x3f,
|
||||
0xef, 0xaf, 0x4f, 0xd8, 0x63, 0xe6, 0xc1, 0x6c, 0x98, 0x45, 0x46, 0x4a, 0x3b, 0x61,
|
||||
0x2c, 0x1e, 0x05, 0x03, 0x65, 0xe8, 0x8d, 0x82, 0x59, 0x56, 0x38, 0x58, 0x2e, 0xc4,
|
||||
0x6f, 0xed
|
||||
])
|
||||
const data = new Uint8Array([0, 12, 66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 255, 64, 0, 0, 7, 77, 101, 114, 99, 117, 114, 121])
|
||||
const username = 'Butlersaurus'
|
||||
const colour = Color('#FF4000')
|
||||
const clientId = 'Mercury'
|
||||
|
||||
// When
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataRequest](ciphertext, KEY)
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataRequest](data)
|
||||
|
||||
// Then
|
||||
expect(unpackedPacket.success).toBe(true)
|
||||
expect(unpackedPacket.username).toBe(username)
|
||||
expect(unpackedPacket.colour).toMatchObject(colour)
|
||||
expect(unpackedPacket.clientId).toBe(clientId)
|
||||
|
|
|
@ -31,22 +31,15 @@ test('Create a user data response (0x0003) packet.', () => {
|
|||
|
||||
test('Parse a user data response (0x0003).', () => {
|
||||
// Given
|
||||
const ciphertext = new Uint8Array([
|
||||
0x56, 0x71, 0x08, 0xf9, 0x2c, 0x4e, 0x41, 0x13, 0xbe, 0x44, 0xba, 0xd9, 0xe4, 0x25,
|
||||
0x14, 0x60, 0x3b, 0x96, 0x7e, 0x0b, 0xbd, 0xac, 0xf0, 0xaf, 0xac, 0xd7, 0x80, 0xe5,
|
||||
0x62, 0xd4, 0x33, 0x10, 0x23, 0x6d, 0x00, 0x3c, 0xae, 0x40, 0x6c, 0xe9, 0x40, 0xfc,
|
||||
0x1c, 0xe0, 0xd3, 0xca, 0x65, 0xea, 0x83, 0x73, 0x5e, 0xd2, 0x67, 0xb2, 0x94, 0x58,
|
||||
0x12, 0x73
|
||||
])
|
||||
const data = new Uint8Array([0, 12, 66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 255, 64, 0, 0, 7, 77, 101, 114, 99, 117, 114, 121])
|
||||
const username = 'Butlersaurus'
|
||||
const colour = Color('#FF4000')
|
||||
const clientId = 'Mercury'
|
||||
|
||||
// When
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataResponse](ciphertext, KEY)
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataResponse](data)
|
||||
|
||||
// Then
|
||||
expect(unpackedPacket.success).toBe(true)
|
||||
expect(unpackedPacket.username).toBe(username)
|
||||
expect(unpackedPacket.colour).toMatchObject(colour)
|
||||
expect(unpackedPacket.clientId).toBe(clientId)
|
||||
|
|
Loading…
Reference in New Issue