added custom encryption/decryption

This commit is contained in:
2021-06-06 23:53:56 -05:00
parent 9ea1e98d0a
commit d2c4c5e57a
3 changed files with 34 additions and 8 deletions

View File

@@ -1,7 +1,8 @@
import { randomBytes } from 'crypto';
import {Int64BE, Uint64BE} from 'int64-buffer';
import { Int64BE, Uint64BE } from 'int64-buffer';
import crypto from 'crypto';
import {TextEncoder} from 'text-encoding';
import { TextEncoder } from 'text-encoding';
import { getCustomEncryption } from '../utils/constants';
import {
be_ui8_array_to_i16,
@@ -164,9 +165,18 @@ export default class packet {
const key = Uint8Array.from(hash.digest());
const nonce = this.buffer.slice(0, 12);
this.buffer = new JSChaCha20(key, nonce, 0).decrypt(
this.buffer.slice(12)
);
const customEncryption = getCustomEncryption();
if (customEncryption) {
this.buffer = await customEncryption.decrypt(
key,
nonce,
this.buffer.slice(12)
);
} else {
this.buffer = new JSChaCha20(key, nonce, 0).decrypt(
this.buffer.slice(12)
);
}
this.decode_offset = packet.HEADER.length;
@@ -278,7 +288,12 @@ export default class packet {
nonce = Uint8Array.from(randomBytes(12));
}
this.buffer = new JSChaCha20(key, nonce, 0).encrypt(this.buffer);
const customEncryption = getCustomEncryption();
if (customEncryption) {
this.buffer = await customEncryption.encrypt(key, nonce, this.buffer);
} else {
this.buffer = new JSChaCha20(key, nonce, 0).encrypt(this.buffer);
}
this.push_buffer(nonce);
return this.buffer;