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