ensure proper type for buffer

This commit is contained in:
2021-06-07 18:44:00 -05:00
parent 362096dd20
commit 43335584c5

View File

@@ -25,7 +25,8 @@ export default class packet {
this.token = token;
}
static HEADER = new TextEncoder().encode('repertory');
static HEADER_STRING = 'repertory';
static HEADER = new TextEncoder().encode(this.HEADER_STRING);
buffer = null;
decode_offset = 0;
@@ -169,25 +170,26 @@ export default class packet {
if (customEncryption) {
this.buffer = Buffer.from(
await customEncryption.decrypt(
key.toString('base64'),
nonce.toString('base64'),
this.buffer.slice(12).toString('base64')
Buffer.from(key).toString('base64'),
Buffer.from(nonce).toString('base64'),
Buffer.from(this.buffer.slice(12)).toString('base64')
),
'base64'
);
} else {
this.buffer = new JSChaCha20(key, nonce, 0).decrypt(
this.buffer.slice(12)
this.buffer = Buffer.from(
new JSChaCha20(key, nonce, 0).decrypt(this.buffer.slice(12))
);
}
this.decode_offset = packet.HEADER.length;
const header = this.buffer.slice(0, 9);
if (header.toString() !== packet.HEADER.toString()) {
if (header.toString() !== packet.HEADER_STRING) {
return Promise.reject(new Error('Header does not match'));
}
this.buffer = new Uint8Array(this.buffer);
return this.buffer;
} catch (e) {
return Promise.reject(e);
@@ -293,13 +295,15 @@ export default class packet {
const customEncryption = getCustomEncryption();
if (customEncryption) {
this.buffer = Buffer.from(
this.buffer = new Uint8Array(
Buffer.from(
await customEncryption.encrypt(
key.toString('base64'),
nonce.toString('base64'),
this.buffer.toString('base64')
Buffer.from(key).toString('base64'),
Buffer.from(nonce).toString('base64'),
Buffer.from(this.buffer).toString('base64')
),
'base64'
)
);
} else {
this.buffer = new JSChaCha20(key, nonce, 0).encrypt(this.buffer);