mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Issue 991: Add CBOR Encode operation
This commit is contained in:
parent
4c3324aea1
commit
ae70cb89ed
6 changed files with 177 additions and 1 deletions
14
package-lock.json
generated
14
package-lock.json
generated
|
@ -2965,6 +2965,15 @@
|
||||||
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=",
|
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"cbor": {
|
||||||
|
"version": "5.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/cbor/-/cbor-5.0.1.tgz",
|
||||||
|
"integrity": "sha512-l4ghwqioCyuAaD3LvY4ONwv8NMuERz62xjbMHGdWBqERJPygVmoFER1b4+VS6iW0rXwoVGuKZPPPTofwWOg3YQ==",
|
||||||
|
"requires": {
|
||||||
|
"bignumber.js": "^9.0.0",
|
||||||
|
"nofilter": "^1.0.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
"chai-nightwatch": {
|
"chai-nightwatch": {
|
||||||
"version": "0.4.0",
|
"version": "0.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/chai-nightwatch/-/chai-nightwatch-0.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/chai-nightwatch/-/chai-nightwatch-0.4.0.tgz",
|
||||||
|
@ -9828,6 +9837,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/nodom/-/nodom-2.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/nodom/-/nodom-2.4.0.tgz",
|
||||||
"integrity": "sha512-qhfYgpoCSi37HLiViMlf94YqMQdvk3n3arI1uGbAWZK9NKCYRSI42W8lATeGloYGLYxb8us1C5rTvtsXjwdWQg=="
|
"integrity": "sha512-qhfYgpoCSi37HLiViMlf94YqMQdvk3n3arI1uGbAWZK9NKCYRSI42W8lATeGloYGLYxb8us1C5rTvtsXjwdWQg=="
|
||||||
},
|
},
|
||||||
|
"nofilter": {
|
||||||
|
"version": "1.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/nofilter/-/nofilter-1.0.3.tgz",
|
||||||
|
"integrity": "sha512-FlUlqwRK6reQCaFLAhMcF+6VkVG2caYjKQY3YsRDTl4/SEch595Qb3oLjJRDr8dkHAAOVj2pOx3VknfnSgkE5g=="
|
||||||
|
},
|
||||||
"nopt": {
|
"nopt": {
|
||||||
"version": "3.0.6",
|
"version": "3.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz",
|
||||||
|
|
|
@ -96,6 +96,7 @@
|
||||||
"bootstrap-colorpicker": "^3.2.0",
|
"bootstrap-colorpicker": "^3.2.0",
|
||||||
"bootstrap-material-design": "^4.1.2",
|
"bootstrap-material-design": "^4.1.2",
|
||||||
"bson": "^4.0.3",
|
"bson": "^4.0.3",
|
||||||
|
"cbor": "^5.0.1",
|
||||||
"chi-squared": "^1.1.0",
|
"chi-squared": "^1.1.0",
|
||||||
"codepage": "^1.14.0",
|
"codepage": "^1.14.0",
|
||||||
"core-js": "^3.6.4",
|
"core-js": "^3.6.4",
|
||||||
|
|
|
@ -61,7 +61,8 @@
|
||||||
"Parse TLV",
|
"Parse TLV",
|
||||||
"CSV to JSON",
|
"CSV to JSON",
|
||||||
"JSON to CSV",
|
"JSON to CSV",
|
||||||
"Avro to JSON"
|
"Avro to JSON",
|
||||||
|
"CBOR Encode"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
41
src/core/operations/CBOREncode.mjs
Normal file
41
src/core/operations/CBOREncode.mjs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/**
|
||||||
|
* @author Danh4 [dan.h4@ncsc.gov.uk]
|
||||||
|
* @copyright Crown Copyright 2020
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Operation from "../Operation.mjs";
|
||||||
|
import Cbor from "cbor";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CBOR Encode operation
|
||||||
|
*/
|
||||||
|
class CBOREncode extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CBOREncode constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = "CBOR Encode";
|
||||||
|
this.module = "Default";
|
||||||
|
this.description = "2";
|
||||||
|
this.infoURL = "https://cbor.io";
|
||||||
|
this.inputType = "JSON";
|
||||||
|
this.outputType = "ArrayBuffer";
|
||||||
|
this.args = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {JSON} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {ArrayBuffer}
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
return new Uint8Array(Cbor.encodeCanonical(input)).buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CBOREncode;
|
|
@ -101,6 +101,7 @@ import "./tests/LuhnChecksum.mjs";
|
||||||
import "./tests/CipherSaber2.mjs";
|
import "./tests/CipherSaber2.mjs";
|
||||||
import "./tests/Colossus.mjs";
|
import "./tests/Colossus.mjs";
|
||||||
import "./tests/ParseObjectIDTimestamp.mjs";
|
import "./tests/ParseObjectIDTimestamp.mjs";
|
||||||
|
import "./tests/CBOREncode.mjs";
|
||||||
|
|
||||||
|
|
||||||
// Cannot test operations that use the File type yet
|
// Cannot test operations that use the File type yet
|
||||||
|
|
118
tests/operations/tests/CBOREncode.mjs
Normal file
118
tests/operations/tests/CBOREncode.mjs
Normal file
|
@ -0,0 +1,118 @@
|
||||||
|
/**
|
||||||
|
* CBOR Encode Tests.
|
||||||
|
*
|
||||||
|
* @author Danh4 [dan.h4@ncsc.gov.uk]
|
||||||
|
*
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "CBOR Encode: Can encode integer",
|
||||||
|
input: "15",
|
||||||
|
expectedOutput: "0f",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CBOR Decode: Can encode decimal",
|
||||||
|
input: "1.5",
|
||||||
|
expectedOutput: "f9 3e 00",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CBOR Encode: Can encode text",
|
||||||
|
input: "\"Text\"",
|
||||||
|
expectedOutput: "64 54 65 78 74",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CBOR Encode: Can encode boolean true",
|
||||||
|
input: "true",
|
||||||
|
expectedOutput: "f5",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CBOR Encode: Can encode boolean false",
|
||||||
|
input: "false",
|
||||||
|
expectedOutput: "f4",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CBOR Encode: Can encode map",
|
||||||
|
input: JSON.stringify({a: 1, b: 2, c: 3}),
|
||||||
|
expectedOutput: "a3 61 61 01 61 62 02 61 63 03",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CBOR Encode: Can encode list",
|
||||||
|
input: "[0,1,2]",
|
||||||
|
expectedOutput: "83 00 01 02",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "CBOR Encode",
|
||||||
|
args: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "To Hex",
|
||||||
|
args: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue