From d2c4c5e57a111355a02a2062ff0e6fdecb299c86 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 6 Jun 2021 23:53:56 -0500 Subject: [PATCH] added custom encryption/decryption --- src/index.js | 3 ++- src/networking/packet.js | 27 +++++++++++++++++++++------ src/utils/constants.js | 12 +++++++++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 23cc332..79df100 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,9 @@ import connection from './networking/connection'; import connection_pool from './networking/connection_pool'; import * as ops from './ops'; -export { default as packet } from './networking/packet'; export * as byte_order from './utils/byte_order'; +export { getCustomEncryption, setCustomEncryption } from './utils/constants'; +export { default as packet } from './networking/packet'; export const connect = async (host_or_ip, port, password) => { const conn = new connection(host_or_ip, port, password); diff --git a/src/networking/packet.js b/src/networking/packet.js index 017e165..2a34bd2 100644 --- a/src/networking/packet.js +++ b/src/networking/packet.js @@ -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; diff --git a/src/utils/constants.js b/src/utils/constants.js index 4b6aee9..8612c15 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -1,6 +1,16 @@ -import {v4 as uuidv4} from 'uuid'; +import { v4 as uuidv4 } from 'uuid'; import _package_json from '../../package.json'; +let customEncryption; + +export const getCustomEncryption = () => { + return customEncryption; +}; + +export const setCustomEncryption = (ce) => { + customEncryption = ce; +}; + export const instance_id = uuidv4(); export const package_json = _package_json; export const get_version = () =>