mirror of
https://github.com/gchq/CyberChef.git
synced 2025-06-17 19:55:18 -04:00
Add Bencode function operation
This commit is contained in:
parent
7ecf8dfdaa
commit
d8d594a493
8 changed files with 299 additions and 1 deletions
|
@ -162,7 +162,9 @@
|
|||
"Typex",
|
||||
"Lorenz",
|
||||
"Colossus",
|
||||
"SIGABA"
|
||||
"SIGABA",
|
||||
"Bencode Encode",
|
||||
"Bencode Decode"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
63
src/core/operations/BencodeDecode.mjs
Normal file
63
src/core/operations/BencodeDecode.mjs
Normal file
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* @author jg42526
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import bencodec from "bencodec";
|
||||
|
||||
/**
|
||||
* URL Decode operation
|
||||
*/
|
||||
class BencodeDecode extends Operation {
|
||||
|
||||
/**
|
||||
* URLDecode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Bencode Decode";
|
||||
this.module = "Encodings";
|
||||
this.description = "Decodes a Bencoded string.<br><br>e.g. <code>7:bencode</code> becomes <code>bencode</code>";
|
||||
this.infoURL = "https://en.wikipedia.org/wiki/Bencode";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
if (input) return toStringRepresentation(bencodec.decode(input, { stringify: true }));
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BencodeDecode;
|
||||
|
||||
/**
|
||||
* Returns string representation of object
|
||||
*/
|
||||
function toStringRepresentation(value) {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === "number" || typeof value === "boolean") {
|
||||
return String(value);
|
||||
}
|
||||
|
||||
if (Array.isArray(value) || (value !== null && typeof value === "object")) {
|
||||
// For arrays and objects, output JSON string
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
// For other types (undefined, null), handle as you see fit, e.g.:
|
||||
return String(value);
|
||||
}
|
55
src/core/operations/BencodeEncode.mjs
Normal file
55
src/core/operations/BencodeEncode.mjs
Normal file
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* @author jg42526
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import bencodec from "bencodec";
|
||||
|
||||
/**
|
||||
* URL Decode operation
|
||||
*/
|
||||
class BencodeEncode extends Operation {
|
||||
|
||||
/**
|
||||
* URLDecode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Bencode Encode";
|
||||
this.module = "Encodings";
|
||||
this.description = "Bencodes a string.<br><br>e.g. <code>bencode</code> becomes <code>7:bencode</code>";
|
||||
this.infoURL = "https://en.wikipedia.org/wiki/Bencode";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
return bencodec.encode(parseValue(input), { stringify: true });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BencodeEncode;
|
||||
|
||||
/**
|
||||
* Parses string, returns appropraite data structure
|
||||
*/
|
||||
function parseValue(str) {
|
||||
const trimmed = str.trim();
|
||||
try {
|
||||
// Attempt to parse with JSON.parse
|
||||
return JSON.parse(trimmed);
|
||||
} catch (e) {
|
||||
// If JSON.parse fails, treat input as a plain string (assuming it's unquoted)
|
||||
return trimmed;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue