ESM: Added Hex ops and created a Hex library.

This commit is contained in:
n1474335 2018-04-02 18:06:48 +01:00
parent 0011e9caa8
commit eeb1d0a891
18 changed files with 391 additions and 196 deletions

View File

@ -5,6 +5,7 @@
*/
import Utils from "./Utils";
import {fromHex} from "./lib/Hex";
/**
* The arguments to operations.
@ -88,7 +89,7 @@ class Ingredient {
case "byteArray":
if (typeof data == "string") {
data = data.replace(/\s+/g, "");
return Utils.fromHex(data);
return fromHex(data);
} else {
return data;
}

View File

@ -7,6 +7,7 @@
import utf8 from "utf8";
import moment from "moment-timezone";
import {fromBase64} from "./lib/Base64";
import {toHexFast, fromHex} from "./lib/Hex";
/**
@ -312,7 +313,7 @@ class Utils {
static convertToByteArray(str, type) {
switch (type.toLowerCase()) {
case "hex":
return Utils.fromHex(str);
return fromHex(str);
case "base64":
return fromBase64(str, null, "byteArray");
case "utf8":
@ -345,7 +346,7 @@ class Utils {
static convertToByteString(str, type) {
switch (type.toLowerCase()) {
case "hex":
return Utils.byteArrayToChars(Utils.fromHex(str));
return Utils.byteArrayToChars(fromHex(str));
case "base64":
return Utils.byteArrayToChars(fromBase64(str, null, "byteArray"));
case "utf8":
@ -519,95 +520,6 @@ class Utils {
}
/**
* Convert a byte array into a hex string.
*
* @param {Uint8Array|byteArray} data
* @param {string} [delim=" "]
* @param {number} [padding=2]
* @returns {string}
*
* @example
* // returns "0a 14 1e"
* Utils.toHex([10,20,30]);
*
* // returns "0a:14:1e"
* Utils.toHex([10,20,30], ":");
*/
static toHex(data, delim=" ", padding=2) {
if (!data) return "";
let output = "";
for (let i = 0; i < data.length; i++) {
output += data[i].toString(16).padStart(padding, "0") + delim;
}
// Add \x or 0x to beginning
if (delim === "0x") output = "0x" + output;
if (delim === "\\x") output = "\\x" + output;
if (delim.length)
return output.slice(0, -delim.length);
else
return output;
}
/**
* Convert a byte array into a hex string as efficiently as possible with no options.
*
* @param {byteArray} data
* @returns {string}
*
* @example
* // returns "0a141e"
* Utils.toHex([10,20,30]);
*/
static toHexFast(data) {
if (!data) return "";
const output = [];
for (let i = 0; i < data.length; i++) {
output.push((data[i] >>> 4).toString(16));
output.push((data[i] & 0x0f).toString(16));
}
return output.join("");
}
/**
* Convert a hex string into a byte array.
*
* @param {string} data
* @param {string} [delim]
* @param {number} [byteLen=2]
* @returns {byteArray}
*
* @example
* // returns [10,20,30]
* Utils.fromHex("0a 14 1e");
*
* // returns [10,20,30]
* Utils.fromHex("0a:14:1e", "Colon");
*/
static fromHex(data, delim, byteLen=2) {
delim = delim || (data.indexOf(" ") >= 0 ? "Space" : "None");
if (delim !== "None") {
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), 16));
}
return output;
}
/**
* Parses CSV data and returns it as a two dimensional array or strings.
*
@ -944,7 +856,7 @@ class Utils {
title='Download ${Utils.escapeHtml(file.fileName)}'
download='${Utils.escapeHtml(file.fileName)}'>&#x1f4be;</a>`;
const hexFileData = Utils.toHexFast(new Uint8Array(file.bytes));
const hexFileData = toHexFast(new Uint8Array(file.bytes));
const switchToInputElem = `<a href='#switchFileToInput${i}'
class='file-switch'

View File

@ -27,8 +27,8 @@ const Categories = [
ops: [
// "To Hexdump",
// "From Hexdump",
// "To Hex",
// "From Hex",
"To Hex",
"From Hex",
// "To Charcode",
// "From Charcode",
// "To Decimal",

View File

@ -90,6 +90,30 @@
}
]
},
"From Hex": {
"module": "Default",
"description": "Converts a hexadecimal byte string back into its raw value.<br><br>e.g. <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code> becomes the UTF-8 encoded string <code>Γειά σου</code>",
"inputType": "string",
"outputType": "byteArray",
"flowControl": false,
"args": [
{
"name": "Delimiter",
"type": "option",
"value": [
"Space",
"Comma",
"Semi-colon",
"Colon",
"Line feed",
"CRLF",
"0x",
"\\x",
"None"
]
}
]
},
"Show Base64 offsets": {
"module": "Default",
"description": "When a string is within a block of data and the whole block is Base64'd, the string itself could be represented in Base64 in three distinct ways depending on its offset within the block.<br><br>This operation shows all possible offsets for a given string so that each possible encoding can be considered.",
@ -189,5 +213,29 @@
]
}
]
},
"To Hex": {
"module": "Default",
"description": "Converts the input string to hexadecimal bytes separated by the specified delimiter.<br><br>e.g. The UTF-8 encoded string <code>Γειά σου</code> becomes <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code>",
"inputType": "ArrayBuffer",
"outputType": "string",
"flowControl": false,
"args": [
{
"name": "Delimiter",
"type": "option",
"value": [
"Space",
"Comma",
"Semi-colon",
"Colon",
"Line feed",
"CRLF",
"0x",
"\\x",
"None"
]
}
]
}
}

View File

@ -7,18 +7,22 @@
*/
import FromBase32 from "../../operations/FromBase32";
import FromBase64 from "../../operations/FromBase64";
import FromHex from "../../operations/FromHex";
import ShowBase64Offsets from "../../operations/ShowBase64Offsets";
import ToBase32 from "../../operations/ToBase32";
import ToBase64 from "../../operations/ToBase64";
import ToHex from "../../operations/ToHex";
const OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
OpModules.Default = {
"From Base32": FromBase32,
"From Base64": FromBase64,
"From Hex": FromHex,
"Show Base64 offsets": ShowBase64Offsets,
"To Base32": ToBase32,
"To Base64": ToBase64,
"To Hex": ToHex,
};
export default OpModules;

104
src/core/lib/Hex.mjs Normal file
View File

@ -0,0 +1,104 @@
/**
* Byte representation functions.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Utils from "../Utils";
/**
* Convert a byte array into a hex string.
*
* @param {Uint8Array|byteArray} data
* @param {string} [delim=" "]
* @param {number} [padding=2]
* @returns {string}
*
* @example
* // returns "0a 14 1e"
* toHex([10,20,30]);
*
* // returns "0a:14:1e"
* toHex([10,20,30], ":");
*/
export function toHex(data, delim=" ", padding=2) {
if (!data) return "";
let output = "";
for (let i = 0; i < data.length; i++) {
output += data[i].toString(16).padStart(padding, "0") + delim;
}
// Add \x or 0x to beginning
if (delim === "0x") output = "0x" + output;
if (delim === "\\x") output = "\\x" + output;
if (delim.length)
return output.slice(0, -delim.length);
else
return output;
}
/**
* Convert a byte array into a hex string as efficiently as possible with no options.
*
* @param {byteArray} data
* @returns {string}
*
* @example
* // returns "0a141e"
* toHex([10,20,30]);
*/
export function toHexFast(data) {
if (!data) return "";
const output = [];
for (let i = 0; i < data.length; i++) {
output.push((data[i] >>> 4).toString(16));
output.push((data[i] & 0x0f).toString(16));
}
return output.join("");
}
/**
* Convert a hex string into a byte array.
*
* @param {string} data
* @param {string} [delim]
* @param {number} [byteLen=2]
* @returns {byteArray}
*
* @example
* // returns [10,20,30]
* fromHex("0a 14 1e");
*
* // returns [10,20,30]
* fromHex("0a:14:1e", "Colon");
*/
export function fromHex(data, delim, byteLen=2) {
delim = delim || (data.indexOf(" ") >= 0 ? "Space" : "None");
if (delim !== "None") {
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), 16));
}
return output;
}
/**
* Hexadecimal delimiters.
*/
export const HEX_DELIM_OPTIONS = ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "0x", "\\x", "None"];

View File

@ -0,0 +1,98 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import {fromHex, HEX_DELIM_OPTIONS} from "../lib/Hex";
import Utils from "../Utils";
/**
* From Hex operation
*/
class FromHex extends Operation {
/**
* FromHex constructor
*/
constructor() {
super();
this.name = "From Hex";
this.module = "Default";
this.description = "Converts a hexadecimal byte string back into its raw value.<br><br>e.g. <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code> becomes the UTF-8 encoded string <code>Γειά σου</code>";
this.inputType = "string";
this.outputType = "byteArray";
this.args = [
{
name: "Delimiter",
type: "option",
value: HEX_DELIM_OPTIONS
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
const delim = args[0] || "Space";
return fromHex(input, delim, 2);
}
/**
* Highlight to Hex
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
const delim = Utils.charRep(args[0] || "Space"),
len = delim === "\r\n" ? 1 : delim.length,
width = len + 2;
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
if (pos[0].start > 1) pos[0].start -= 2;
else pos[0].start = 0;
if (pos[0].end > 1) pos[0].end -= 2;
else pos[0].end = 0;
}
pos[0].start = pos[0].start === 0 ? 0 : Math.round(pos[0].start / width);
pos[0].end = pos[0].end === 0 ? 0 : Math.ceil(pos[0].end / width);
return pos;
}
/**
* Highlight from Hex
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
const delim = Utils.charRep(args[0] || "Space"),
len = delim === "\r\n" ? 1 : delim.length;
pos[0].start = pos[0].start * (2 + len);
pos[0].end = pos[0].end * (2 + len) - len;
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
pos[0].start += 2;
pos[0].end += 2;
}
return pos;
}
}
export default FromHex;

View File

@ -0,0 +1,98 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import {toHex, HEX_DELIM_OPTIONS} from "../lib/Hex";
import Utils from "../Utils";
/**
* To Hex operation
*/
class ToHex extends Operation {
/**
* ToHex constructor
*/
constructor() {
super();
this.name = "To Hex";
this.module = "Default";
this.description = "Converts the input string to hexadecimal bytes separated by the specified delimiter.<br><br>e.g. The UTF-8 encoded string <code>Γειά σου</code> becomes <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code>";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
name: "Delimiter",
type: "option",
value: HEX_DELIM_OPTIONS
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const delim = Utils.charRep(args[0] || "Space");
return toHex(new Uint8Array(input), delim, 2);
}
/**
* Highlight to Hex
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
const delim = Utils.charRep(args[0] || "Space"),
len = delim === "\r\n" ? 1 : delim.length;
pos[0].start = pos[0].start * (2 + len);
pos[0].end = pos[0].end * (2 + len) - len;
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
pos[0].start += 2;
pos[0].end += 2;
}
return pos;
}
/**
* Highlight from Hex
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
const delim = Utils.charRep(args[0] || "Space"),
len = delim === "\r\n" ? 1 : delim.length,
width = len + 2;
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
if (pos[0].start > 1) pos[0].start -= 2;
else pos[0].start = 0;
if (pos[0].end > 1) pos[0].end -= 2;
else pos[0].end = 0;
}
pos[0].start = pos[0].start === 0 ? 0 : Math.round(pos[0].start / width);
pos[0].end = pos[0].end === 0 ? 0 : Math.ceil(pos[0].end / width);
return pos;
}
}
export default ToHex;

View File

@ -3,11 +3,15 @@ import FromBase64 from "./FromBase64";
import ToBase32 from "./ToBase32";
import FromBase32 from "./FromBase32";
import ShowBase64Offsets from "./ShowBase64Offsets";
import ToHex from "./ToHex";
import FromHex from "./FromHex";
export {
ToBase64,
FromBase64,
ToBase32,
FromBase32,
ShowBase64Offsets
ShowBase64Offsets,
ToHex,
FromHex
};

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js";
import {toHex} from "../lib/Hex";
/**
@ -166,7 +167,7 @@ const BitwiseOp = {
if (crib && resultUtf8.toLowerCase().indexOf(crib) < 0) continue;
if (printKey) record += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
if (outputHex) {
record += Utils.toHex(result);
record += toHex(result);
} else {
record += Utils.printable(resultUtf8, false);
}

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js";
import {toHex, fromHex} from "../lib/Hex";
/**
@ -17,42 +18,12 @@ const ByteRepr = {
* @default
*/
DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF"],
/**
* @constant
* @default
*/
HEX_DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "0x", "\\x", "None"],
/**
* @constant
* @default
*/
BIN_DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "None"],
/**
* To Hex operation.
*
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
runToHex: function(input, args) {
const delim = Utils.charRep(args[0] || "Space");
return Utils.toHex(new Uint8Array(input), delim, 2);
},
/**
* From Hex operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
runFromHex: function(input, args) {
const delim = args[0] || "Space";
return Utils.fromHex(input, delim, 2);
},
/**
* To Octal operation.
@ -171,59 +142,6 @@ const ByteRepr = {
},
/**
* Highlight to hex
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightTo: function(pos, args) {
let delim = Utils.charRep(args[0] || "Space"),
len = delim === "\r\n" ? 1 : delim.length;
pos[0].start = pos[0].start * (2 + len);
pos[0].end = pos[0].end * (2 + len) - len;
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
pos[0].start += 2;
pos[0].end += 2;
}
return pos;
},
/**
* Highlight from hex
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightFrom: function(pos, args) {
let delim = Utils.charRep(args[0] || "Space"),
len = delim === "\r\n" ? 1 : delim.length,
width = len + 2;
// 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
if (delim === "0x" || delim === "\\x") {
if (pos[0].start > 1) pos[0].start -= 2;
else pos[0].start = 0;
if (pos[0].end > 1) pos[0].end -= 2;
else pos[0].end = 0;
}
pos[0].start = pos[0].start === 0 ? 0 : Math.round(pos[0].start / width);
pos[0].end = pos[0].end === 0 ? 0 : Math.ceil(pos[0].end / width);
return pos;
},
/**
* To Decimal operation.
*
@ -357,7 +275,7 @@ const ByteRepr = {
const convert = args[0];
const spaces = args[1];
if (convert === "All chars") {
let result = "|" + Utils.toHex(input) + "|";
let result = "|" + toHex(input) + "|";
if (!spaces) result = result.replace(/ /g, "");
return result;
}
@ -373,7 +291,7 @@ const ByteRepr = {
output += "|";
inHex = true;
} else if (spaces) output += " ";
output += Utils.toHex([b]);
output += toHex([b]);
} else {
if (inHex) {
output += "|";
@ -403,7 +321,7 @@ const ByteRepr = {
output.push(Utils.ord(input[i++]));
// Add match
const bytes = Utils.fromHex(m[1]);
const bytes = fromHex(m[1]);
if (bytes) {
for (let a = 0; a < bytes.length;)
output.push(bytes[a++]);

View File

@ -1,5 +1,6 @@
import Utils from "../Utils.js";
import {toBase64} from "../lib/Base64";
import {toHexFast} from "../lib/Hex";
import CryptoJS from "crypto-js";
import forge from "imports-loader?jQuery=>null!node-forge/dist/forge.min.js";
import {blowfish as Blowfish} from "sladex-blowfish";
@ -403,7 +404,7 @@ DES uses a key length of 8 bytes (64 bits).`;
cipherMode: Cipher._BLOWFISH_MODE_LOOKUP[mode]
});
return outputType === "Hex" ? Utils.toHexFast(Utils.strToByteArray(result)) : result;
return outputType === "Hex" ? toHexFast(Utils.strToByteArray(result)) : result;
},

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js";
import {toHex, fromHex} from "../lib/Hex";
/**
@ -52,7 +53,7 @@ const Endian = {
// Convert input to raw data based on specified data format
switch (dataFormat) {
case "Hex":
data = Utils.fromHex(input);
data = fromHex(input);
break;
case "Raw":
data = Utils.strToByteArray(input);
@ -86,7 +87,7 @@ const Endian = {
// Convert data back to specified data format
switch (dataFormat) {
case "Hex":
return Utils.toHex(result);
return toHex(result);
case "Raw":
return Utils.byteArrayToUtf8(result);
default:

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js";
import {fromHex} from "../lib/Hex";
/**
@ -82,7 +83,7 @@ const Hexdump = {
block, line;
while ((block = regex.exec(input))) {
line = Utils.fromHex(block[1].replace(/-/g, " "));
line = fromHex(block[1].replace(/-/g, " "));
for (let i = 0; i < line.length; i++) {
output.push(line[i]);
}

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js";
import {toHex, fromHex} from "../lib/Hex";
import Checksum from "./Checksum.js";
import {BigInteger} from "jsbn";
@ -283,7 +284,7 @@ const IP = {
baIp.push(decimal & 255);
break;
case "Hex":
baIp = Utils.fromHex(lines[i]);
baIp = fromHex(lines[i]);
break;
default:
throw "Unsupported input IP format";
@ -445,7 +446,7 @@ const IP = {
output;
if (format === "Hex") {
input = Utils.fromHex(input);
input = fromHex(input);
} else if (format === "Raw") {
input = Utils.strToByteArray(input);
} else {
@ -516,7 +517,7 @@ const IP = {
"<tr><td>Destination IP address</td><td>" + IP._ipv4ToStr(dstIP) + "</td></tr>";
if (ihl > 5) {
output += "<tr><td>Options</td><td>" + Utils.toHex(options) + "</td></tr>";
output += "<tr><td>Options</td><td>" + toHex(options) + "</td></tr>";
}
return output + "</table>";

View File

@ -3,6 +3,7 @@ import removeEXIF from "../vendor/remove-exif.js";
import Utils from "../Utils.js";
import FileType from "./FileType.js";
import {fromBase64, toBase64} from "../lib/Base64";
import {fromHex} from "../lib/Hex";
/**
@ -92,7 +93,7 @@ const Image = {
// Convert input to raw bytes
switch (inputFormat) {
case "Hex":
input = Utils.fromHex(input);
input = fromHex(input);
break;
case "Base64":
// Don't trust the Base64 entered by the user.

View File

@ -1,5 +1,6 @@
import Utils from "../Utils.js";
import {fromBase64} from "../lib/Base64";
import {toHex, fromHex} from "../lib/Hex";
import * as r from "jsrsasign";
@ -44,10 +45,10 @@ const PublicKey = {
cert.readCertPEM(input);
break;
case "Base64":
cert.readCertHex(Utils.toHex(fromBase64(input, null, "byteArray"), ""));
cert.readCertHex(toHex(fromBase64(input, null, "byteArray"), ""));
break;
case "Raw":
cert.readCertHex(Utils.toHex(Utils.strToByteArray(input), ""));
cert.readCertHex(toHex(Utils.strToByteArray(input), ""));
break;
default:
throw "Undefined input format";
@ -305,7 +306,7 @@ ${extensions}`;
* @returns {string}
*/
_formatByteStr: function(byteStr, length, indent) {
byteStr = Utils.toHex(Utils.fromHex(byteStr), ":");
byteStr = toHex(fromHex(byteStr), ":");
length = length * 3;
let output = "";

View File

@ -1,4 +1,5 @@
import Utils from "../Utils.js";
import {fromHex} from "../lib/Hex";
import jsesc from "jsesc";
@ -380,8 +381,8 @@ const StrUtils = {
}
if (inputType === "Hex") {
samples[0] = Utils.fromHex(samples[0]);
samples[1] = Utils.fromHex(samples[1]);
samples[0] = fromHex(samples[0]);
samples[1] = fromHex(samples[1]);
} else {
samples[0] = Utils.strToByteArray(samples[0]);
samples[1] = Utils.strToByteArray(samples[1]);