mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 15:56:16 -04:00
ESM: Added Hex ops and created a Hex library.
This commit is contained in:
parent
0011e9caa8
commit
eeb1d0a891
18 changed files with 391 additions and 196 deletions
98
src/core/operations/FromHex.mjs
Normal file
98
src/core/operations/FromHex.mjs
Normal 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;
|
98
src/core/operations/ToHex.mjs
Normal file
98
src/core/operations/ToHex.mjs
Normal 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;
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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++]);
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
@ -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>";
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 = "";
|
||||
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue