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

@@ -3,8 +3,9 @@ import connection from './networking/connection';
import connection_pool from './networking/connection_pool'; import connection_pool from './networking/connection_pool';
import * as ops from './ops'; import * as ops from './ops';
export { default as packet } from './networking/packet';
export * as byte_order from './utils/byte_order'; 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) => { export const connect = async (host_or_ip, port, password) => {
const conn = new connection(host_or_ip, port, password); const conn = new connection(host_or_ip, port, password);

View File

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

View File

@@ -1,6 +1,16 @@
import {v4 as uuidv4} from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import _package_json from '../../package.json'; 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 instance_id = uuidv4();
export const package_json = _package_json; export const package_json = _package_json;
export const get_version = () => export const get_version = () =>