Skip to content

Commit 88e3c2c

Browse files
committed
Merge branch 'issue-991' of https://github.com/Danh4/CyberChef into Danh4-issue-991
2 parents 5029356 + 209fc07 commit 88e3c2c

File tree

8 files changed

+367
-1
lines changed

8 files changed

+367
-1
lines changed

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"browserify-zlib": "^0.2.0",
9999
"bson": "^4.2.2",
100100
"buffer": "^6.0.3",
101+
"cbor": "^5.0.1",
101102
"chi-squared": "^1.1.0",
102103
"codepage": "^1.14.0",
103104
"core-js": "^3.8.3",

src/core/config/Categories.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
"Parse TLV",
6262
"CSV to JSON",
6363
"JSON to CSV",
64-
"Avro to JSON"
64+
"Avro to JSON",
65+
"CBOR Encode",
66+
"CBOR Decode"
6567
]
6668
},
6769
{

src/core/operations/CBORDecode.mjs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author Danh4 [[email protected]]
3+
* @copyright Crown Copyright 2020
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import Cbor from "cbor";
9+
10+
/**
11+
* CBOR Decode operation
12+
*/
13+
class CBORDecode extends Operation {
14+
15+
/**
16+
* CBORDecode constructor
17+
*/
18+
constructor() {
19+
super();
20+
21+
this.name = "CBOR Decode";
22+
this.module = "Default";
23+
this.description = "Decode Concise Binary Object Representation (CBOR) data format (RFC7049) to JSON.";
24+
this.infoURL = "https://cbor.io";
25+
this.inputType = "ArrayBuffer";
26+
this.outputType = "JSON";
27+
this.args = [
28+
];
29+
}
30+
31+
/**
32+
* @param {ArrayBuffer} input
33+
* @param {Object[]} args
34+
* @returns {JSON}
35+
*/
36+
run(input, args) {
37+
return Cbor.decodeFirstSync(Buffer.from(input).toString("hex"));
38+
}
39+
40+
}
41+
42+
export default CBORDecode;

src/core/operations/CBOREncode.mjs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @author Danh4 [[email protected]]
3+
* @copyright Crown Copyright 2020
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import Cbor from "cbor";
9+
10+
/**
11+
* CBOR Encode operation
12+
*/
13+
class CBOREncode extends Operation {
14+
15+
/**
16+
* CBOREncode constructor
17+
*/
18+
constructor() {
19+
super();
20+
21+
this.name = "CBOR Encode";
22+
this.module = "Default";
23+
this.description = "2";
24+
this.infoURL = "https://cbor.io";
25+
this.inputType = "JSON";
26+
this.outputType = "ArrayBuffer";
27+
this.args = [];
28+
}
29+
30+
/**
31+
* @param {JSON} input
32+
* @param {Object[]} args
33+
* @returns {ArrayBuffer}
34+
*/
35+
run(input, args) {
36+
return new Uint8Array(Cbor.encodeCanonical(input)).buffer;
37+
}
38+
39+
}
40+
41+
export default CBOREncode;

tests/operations/index.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ import "./tests/Colossus.mjs";
102102
import "./tests/ParseObjectIDTimestamp.mjs";
103103
import "./tests/Unicode.mjs";
104104
import "./tests/RSA.mjs";
105+
import "./tests/CBOREncode.mjs";
106+
import "./tests/CBORDecode.mjs";
107+
105108

106109
// Cannot test operations that use the File type yet
107110
// import "./tests/SplitColourChannels.mjs";

tests/operations/tests/CBORDecode.mjs

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* From Hex Tests.
3+
*
4+
* @author Danh4 [[email protected]]
5+
*
6+
* @copyright Crown Copyright 2019
7+
* @license Apache-2.0
8+
*/
9+
10+
import TestRegister from "../../lib/TestRegister.mjs";
11+
12+
TestRegister.addTests([
13+
{
14+
name: "CBOR Decode: Can decode integer",
15+
input: "0f",
16+
expectedOutput: "15",
17+
recipeConfig: [
18+
{
19+
op: "From Hex",
20+
args: []
21+
},
22+
{
23+
op: "CBOR Decode",
24+
args: []
25+
}
26+
]
27+
},
28+
{
29+
name: "CBOR Decode: Can decode decimal",
30+
input: "f9 3e 00",
31+
expectedOutput: "1.5",
32+
recipeConfig: [
33+
{
34+
op: "From Hex",
35+
args: []
36+
},
37+
{
38+
op: "CBOR Decode",
39+
args: []
40+
}
41+
]
42+
},
43+
{
44+
name: "From Hex: Can decode text",
45+
input: "64 54 65 78 74",
46+
expectedOutput: "\"Text\"",
47+
recipeConfig: [
48+
{
49+
op: "From Hex",
50+
args: []
51+
},
52+
{
53+
op: "CBOR Decode",
54+
args: []
55+
}
56+
]
57+
},
58+
{
59+
name: "From Hex: Can decode boolean true",
60+
input: "f5",
61+
expectedOutput: "true",
62+
recipeConfig: [
63+
{
64+
op: "From Hex",
65+
args: []
66+
},
67+
{
68+
op: "CBOR Decode",
69+
args: []
70+
}
71+
]
72+
},
73+
{
74+
name: "From Hex: Can decode boolean false",
75+
input: "f4",
76+
expectedOutput: "false",
77+
recipeConfig: [
78+
{
79+
op: "From Hex",
80+
args: []
81+
},
82+
{
83+
op: "CBOR Decode",
84+
args: []
85+
}
86+
]
87+
},
88+
{
89+
name: "From Hex: Can decode map",
90+
input: "a3 61 61 01 61 62 02 61 63 03",
91+
expectedOutput: JSON.stringify({a: 1, b: 2, c: 3}),
92+
recipeConfig: [
93+
{
94+
op: "From Hex",
95+
args: []
96+
},
97+
{
98+
op: "CBOR Decode",
99+
args: []
100+
},
101+
{
102+
op: "JSON Minify",
103+
args: []
104+
}
105+
]
106+
},
107+
{
108+
name: "From Hex: Can decode list",
109+
input: "83 00 01 02",
110+
expectedOutput: "[0,1,2]",
111+
recipeConfig: [
112+
{
113+
op: "From Hex",
114+
args: []
115+
},
116+
{
117+
op: "CBOR Decode",
118+
args: []
119+
},
120+
{
121+
op: "JSON Minify",
122+
args: []
123+
}
124+
]
125+
},
126+
{
127+
name: "From Hex: Can round trip with encode",
128+
input: JSON.stringify({a: 1, b: false, c: [1, 2, 3]}),
129+
expectedOutput: JSON.stringify({a: 1, b: false, c: [1, 2, 3]}),
130+
recipeConfig: [
131+
{
132+
op: "CBOR Encode",
133+
args: []
134+
},
135+
{
136+
op: "CBOR Decode",
137+
args: []
138+
},
139+
{
140+
op: "JSON Minify",
141+
args: []
142+
}
143+
]
144+
}
145+
]);

0 commit comments

Comments
 (0)