Skip to content

Commit

Permalink
Added 'Binary' key option to all bitwise operations. Closes #398
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Nov 9, 2018
1 parent 3c4893d commit 42e8813
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 34 deletions.
9 changes: 7 additions & 2 deletions src/core/Utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import moment from "moment-timezone";
import {fromBase64} from "./lib/Base64";
import {fromHex} from "./lib/Hex";
import {fromDecimal} from "./lib/Decimal";
import {fromBinary} from "./lib/Binary";


/**
Expand Down Expand Up @@ -298,7 +299,7 @@ class Utils {
* Accepts hex, Base64, UTF8 and Latin1 strings.
*
* @param {string} str
* @param {string} type - One of "Hex", "Decimal", "Base64", "UTF8" or "Latin1"
* @param {string} type - One of "Binary", "Hex", "Decimal", "Base64", "UTF8" or "Latin1"
* @returns {byteArray}
*
* @example
Expand All @@ -313,6 +314,8 @@ class Utils {
*/
static convertToByteArray(str, type) {
switch (type.toLowerCase()) {
case "binary":
return fromBinary(str);
case "hex":
return fromHex(str);
case "decimal":
Expand All @@ -333,7 +336,7 @@ class Utils {
* Accepts hex, Base64, UTF8 and Latin1 strings.
*
* @param {string} str
* @param {string} type - One of "Hex", "Decimal", "Base64", "UTF8" or "Latin1"
* @param {string} type - One of "Binary", "Hex", "Decimal", "Base64", "UTF8" or "Latin1"
* @returns {string}
*
* @example
Expand All @@ -348,6 +351,8 @@ class Utils {
*/
static convertToByteString(str, type) {
switch (type.toLowerCase()) {
case "binary":
return Utils.byteArrayToChars(fromBinary(str));
case "hex":
return Utils.byteArrayToChars(fromHex(str));
case "decimal":
Expand Down
70 changes: 70 additions & 0 deletions src/core/lib/Binary.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Binary functions.
*
* @author n1474335 [[email protected]]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

import Utils from "../Utils";


/**
* Convert a byte array into a binary string.
*
* @param {Uint8Array|byteArray} data
* @param {string} [delim="Space"]
* @param {number} [padding=8]
* @returns {string}
*
* @example
* // returns "00010000 00100000 00110000"
* toHex([10,20,30]);
*
* // returns "00010000 00100000 00110000"
* toHex([10,20,30], ":");
*/
export function toBinary(data, delim="Space", padding=8) {
if (!data) return "";

delim = Utils.charRep(delim);
let output = "";

for (let i = 0; i < data.length; i++) {
output += data[i].toString(2).padStart(padding, "0") + delim;
}

if (delim.length) {
return output.slice(0, -delim.length);
} else {
return output;
}
}


/**
* Convert a binary string into a byte array.
*
* @param {string} data
* @param {string} [delim]
* @param {number} [byteLen=8]
* @returns {byteArray}
*
* @example
* // returns [10,20,30]
* fromBinary("00010000 00100000 00110000");
*
* // returns [10,20,30]
* fromBinary("00010000:00100000:00110000", "Colon");
*/
export function fromBinary(data, delim="Space", byteLen=8) {
const delimRegex = Utils.regexRep(delim);
data = data.replace(delimRegex, "");

const output = [];
for (let i = 0; i < data.length; i += byteLen) {
output.push(parseInt(data.substr(i, byteLen), 2));
}
return output;
}

6 changes: 6 additions & 0 deletions src/core/lib/BitwiseOp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ export function sub(operand, key) {
const result = operand - key;
return (result < 0) ? 256 + result : result;
}


/**
* Delimiter options for bitwise operations
*/
export const BITWISE_OP_DELIMS = ["Hex", "Decimal", "Binary", "Base64", "UTF8", "Latin1"];
4 changes: 2 additions & 2 deletions src/core/operations/ADD.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Operation from "../Operation";
import Utils from "../Utils";
import { bitOp, add } from "../lib/BitwiseOp";
import { bitOp, add, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";

/**
* ADD operation
Expand All @@ -30,7 +30,7 @@ class ADD extends Operation {
"name": "Key",
"type": "toggleString",
"value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
"toggleValues": BITWISE_OP_DELIMS
}
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/AND.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Operation from "../Operation";
import Utils from "../Utils";
import { bitOp, and } from "../lib/BitwiseOp";
import { bitOp, and, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";

/**
* AND operation
Expand All @@ -30,7 +30,7 @@ class AND extends Operation {
"name": "Key",
"type": "toggleString",
"value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
"toggleValues": BITWISE_OP_DELIMS
}
];
}
Expand Down
11 changes: 2 additions & 9 deletions src/core/operations/FromBinary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import {BIN_DELIM_OPTIONS} from "../lib/Delim";
import {fromBinary} from "../lib/Binary";

/**
* From Binary operation
Expand Down Expand Up @@ -77,15 +78,7 @@ class FromBinary extends Operation {
* @returns {byteArray}
*/
run(input, args) {
const delimRegex = Utils.regexRep(args[0] || "Space");
input = input.replace(delimRegex, "");

const output = [];
const byteLen = 8;
for (let i = 0; i < input.length; i += byteLen) {
output.push(parseInt(input.substr(i, byteLen), 2));
}
return output;
return fromBinary(input, args[0]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/OR.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Operation from "../Operation";
import Utils from "../Utils";
import { bitOp, or } from "../lib/BitwiseOp";
import { bitOp, or, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";

/**
* OR operation
Expand All @@ -30,7 +30,7 @@ class OR extends Operation {
"name": "Key",
"type": "toggleString",
"value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
"toggleValues": BITWISE_OP_DELIMS
}
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/SUB.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Operation from "../Operation";
import Utils from "../Utils";
import { bitOp, sub } from "../lib/BitwiseOp";
import { bitOp, sub, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";

/**
* SUB operation
Expand All @@ -30,7 +30,7 @@ class SUB extends Operation {
"name": "Key",
"type": "toggleString",
"value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
"toggleValues": BITWISE_OP_DELIMS
}
];
}
Expand Down
15 changes: 2 additions & 13 deletions src/core/operations/ToBinary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import {BIN_DELIM_OPTIONS} from "../lib/Delim";
import {toBinary} from "../lib/Binary";

/**
* To Binary operation
Expand Down Expand Up @@ -40,19 +41,7 @@ class ToBinary extends Operation {
* @returns {string}
*/
run(input, args) {
const delim = Utils.charRep(args[0] || "Space"),
padding = 8;
let output = "";

for (let i = 0; i < input.length; i++) {
output += input[i].toString(2).padStart(padding, "0") + delim;
}

if (delim.length) {
return output.slice(0, -delim.length);
} else {
return output;
}
return toBinary(input, args[0]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/operations/XOR.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Operation from "../Operation";
import Utils from "../Utils";
import { bitOp, xor } from "../lib/BitwiseOp";
import { bitOp, xor, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";

/**
* XOR operation
Expand All @@ -30,7 +30,7 @@ class XOR extends Operation {
"name": "Key",
"type": "toggleString",
"value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
"toggleValues": BITWISE_OP_DELIMS
},
{
"name": "Scheme",
Expand Down

0 comments on commit 42e8813

Please sign in to comment.