Return ciphertext rather than decrypted content
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user