mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
add Set Difference operation
This commit is contained in:
parent
03ecaa81f7
commit
852c95a994
9 changed files with 176 additions and 6 deletions
|
@ -210,6 +210,25 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"Set Difference": {
|
||||||
|
"module": "Default",
|
||||||
|
"description": "Get the Difference of two sets",
|
||||||
|
"inputType": "string",
|
||||||
|
"outputType": "string",
|
||||||
|
"flowControl": false,
|
||||||
|
"args": [
|
||||||
|
{
|
||||||
|
"name": "Sample delimiter",
|
||||||
|
"type": "binaryString",
|
||||||
|
"value": "\\n\\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Item delimiter",
|
||||||
|
"type": "binaryString",
|
||||||
|
"value": ","
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"Set Intersection": {
|
"Set Intersection": {
|
||||||
"module": "Default",
|
"module": "Default",
|
||||||
"description": "Get the intersection of two sets",
|
"description": "Get the intersection of two sets",
|
||||||
|
|
|
@ -9,6 +9,7 @@ import FromBase32 from "../../operations/FromBase32";
|
||||||
import FromBase64 from "../../operations/FromBase64";
|
import FromBase64 from "../../operations/FromBase64";
|
||||||
import FromHex from "../../operations/FromHex";
|
import FromHex from "../../operations/FromHex";
|
||||||
import RawDeflate from "../../operations/RawDeflate";
|
import RawDeflate from "../../operations/RawDeflate";
|
||||||
|
import SetDifference from "../../operations/SetDifference";
|
||||||
import SetIntersection from "../../operations/SetIntersection";
|
import SetIntersection from "../../operations/SetIntersection";
|
||||||
import SetOps from "../../operations/SetOps";
|
import SetOps from "../../operations/SetOps";
|
||||||
import SetUnion from "../../operations/SetUnion";
|
import SetUnion from "../../operations/SetUnion";
|
||||||
|
@ -24,6 +25,7 @@ OpModules.Default = {
|
||||||
"From Base64": FromBase64,
|
"From Base64": FromBase64,
|
||||||
"From Hex": FromHex,
|
"From Hex": FromHex,
|
||||||
"Raw Deflate": RawDeflate,
|
"Raw Deflate": RawDeflate,
|
||||||
|
"Set Difference": SetDifference,
|
||||||
"Set Intersection": SetIntersection,
|
"Set Intersection": SetIntersection,
|
||||||
"": SetOps,
|
"": SetOps,
|
||||||
"Set Union": SetUnion,
|
"Set Union": SetUnion,
|
||||||
|
|
86
src/core/operations/SetDifference.mjs
Normal file
86
src/core/operations/SetDifference.mjs
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
/**
|
||||||
|
* @author d98762625 [d98762625@gmail.com]
|
||||||
|
* @copyright Crown Copyright 2018
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Utils from "../Utils";
|
||||||
|
import Operation from "../Operation";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Difference operation
|
||||||
|
*/
|
||||||
|
class SetDifference extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Difference constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = "Set Difference";
|
||||||
|
this.module = "Default";
|
||||||
|
this.description = "Get the Difference of two sets";
|
||||||
|
this.inputType = "string";
|
||||||
|
this.outputType = "string";
|
||||||
|
this.args = [
|
||||||
|
{
|
||||||
|
name: "Sample delimiter",
|
||||||
|
type: "binaryString",
|
||||||
|
value: Utils.escapeHtml("\\n\\n")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Item delimiter",
|
||||||
|
type: "binaryString",
|
||||||
|
value: ","
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate input length
|
||||||
|
* @param {Object[]} sets
|
||||||
|
* @throws {Error} if not two sets
|
||||||
|
*/
|
||||||
|
validateSampleNumbers(sets) {
|
||||||
|
if (!sets || (sets.length !== 2)) {
|
||||||
|
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the difference operation
|
||||||
|
* @param input
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
[this.sampleDelim, this.itemDelimiter] = args;
|
||||||
|
const sets = input.split(this.sampleDelim);
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.validateSampleNumbers(sets);
|
||||||
|
} catch (e) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Utils.escapeHtml(this.runSetDifferencez(...sets.map(s => s.split(this.itemDelimiter))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get elements in set a that are not in set b
|
||||||
|
*
|
||||||
|
* @param {Object[]} a
|
||||||
|
* @param {Object[]} b
|
||||||
|
* @returns {Object[]}
|
||||||
|
*/
|
||||||
|
runSetDifference(a, b) {
|
||||||
|
return a
|
||||||
|
.filter((item) => {
|
||||||
|
return b.indexOf(item) === -1;
|
||||||
|
})
|
||||||
|
.join(this.itemDelimiter);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SetDifference;
|
|
@ -1,3 +1,9 @@
|
||||||
|
/**
|
||||||
|
* @author d98762625 [d98762625@gmail.com]
|
||||||
|
* @copyright Crown Copyright 2018
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
import Utils from "../Utils";
|
import Utils from "../Utils";
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import Gunzip from "./Gunzip";
|
||||||
import Gzip from "./Gzip";
|
import Gzip from "./Gzip";
|
||||||
import RawDeflate from "./RawDeflate";
|
import RawDeflate from "./RawDeflate";
|
||||||
import RawInflate from "./RawInflate";
|
import RawInflate from "./RawInflate";
|
||||||
|
import SetDifference from "./SetDifference";
|
||||||
import SetIntersection from "./SetIntersection";
|
import SetIntersection from "./SetIntersection";
|
||||||
import SetOps from "./SetOps";
|
import SetOps from "./SetOps";
|
||||||
import SetUnion from "./SetUnion";
|
import SetUnion from "./SetUnion";
|
||||||
|
@ -32,6 +33,7 @@ export {
|
||||||
Gzip,
|
Gzip,
|
||||||
RawDeflate,
|
RawDeflate,
|
||||||
RawInflate,
|
RawInflate,
|
||||||
|
SetDifference,
|
||||||
SetIntersection,
|
SetIntersection,
|
||||||
SetOps,
|
SetOps,
|
||||||
SetUnion,
|
SetUnion,
|
||||||
|
|
|
@ -49,8 +49,7 @@ import "./tests/operations/Base64";
|
||||||
// import "./tests/operations/SeqUtils.js";
|
// import "./tests/operations/SeqUtils.js";
|
||||||
import "./tests/operations/SetUnion";
|
import "./tests/operations/SetUnion";
|
||||||
import "./tests/operations/SetIntersection";
|
import "./tests/operations/SetIntersection";
|
||||||
|
import "./tests/operations/SetDifference";
|
||||||
|
|
||||||
|
|
||||||
let allTestsPassing = true;
|
let allTestsPassing = true;
|
||||||
const testStatusCounts = {
|
const testStatusCounts = {
|
||||||
|
|
56
test/tests/operations/SetDifference.mjs
Normal file
56
test/tests/operations/SetDifference.mjs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
/**
|
||||||
|
* Set Difference tests.
|
||||||
|
*
|
||||||
|
* @author d98762625
|
||||||
|
*
|
||||||
|
* @copyright Crown Copyright 2018
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../TestRegister";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Set Difference",
|
||||||
|
input: "1 2 3 4 5\n\n3 4 5 6 7",
|
||||||
|
expectedOutput: "1 2",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Set Difference",
|
||||||
|
args: ["\n\n", " "],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Set Difference: wrong sample count",
|
||||||
|
input: "1 2 3 4 5_3_4 5 6 7",
|
||||||
|
expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Set Difference",
|
||||||
|
args: [" ", "_"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Set Difference: item delimiter",
|
||||||
|
input: "1;2;3;4;5\n\n3;4;5;6;7",
|
||||||
|
expectedOutput: "1;2",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Set Difference",
|
||||||
|
args: ["\n\n", ";"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Set Difference: sample delimiter",
|
||||||
|
input: "1;2;3;4;5===3;4;5;6;7",
|
||||||
|
expectedOutput: "1;2",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Set Difference",
|
||||||
|
args: ["===", ";"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
|
@ -1,9 +1,9 @@
|
||||||
/**
|
/**
|
||||||
* Set Operations tests.
|
* Set Intersection tests.
|
||||||
*
|
*
|
||||||
* @author d98762625
|
* @author d98762625
|
||||||
*
|
*
|
||||||
* @copyright Crown Copyright 2017
|
* @copyright Crown Copyright 2018
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
import TestRegister from "../../TestRegister";
|
import TestRegister from "../../TestRegister";
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/**
|
/**
|
||||||
* Set Operations tests.
|
* Set Union tests.
|
||||||
*
|
*
|
||||||
* @author d98762625
|
* @author d98762625
|
||||||
*
|
*
|
||||||
* @copyright Crown Copyright 2017
|
* @copyright Crown Copyright 2018
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
import TestRegister from "../../TestRegister";
|
import TestRegister from "../../TestRegister";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue