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

@ -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]);