Skip to content

Commit 2d9e877

Browse files
committed
Updated Base85 operations for latest CyberChef version
1 parent 5aa13f2 commit 2d9e877

4 files changed

Lines changed: 244 additions & 0 deletions

File tree

src/core/config/Categories.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"From Base32",
2626
"To Base58",
2727
"From Base58",
28+
"To Base85",
29+
"From Base85",
2830
"To Base",
2931
"From Base",
3032
"To BCD",

src/core/lib/Base85.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Base85 resources.
3+
*
4+
* @author PenguinGeorge [george@penguingeorge.com]
5+
* @copyright Crown Copyright 2018
6+
* @license Apache-2.0
7+
*/
8+
9+
/**
10+
* Base85 alphabet options.
11+
*/
12+
export const ALPHABET_OPTIONS = [
13+
{
14+
name: "Standard",
15+
value: "!&quot;#$%&&apos;()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[&bsol;]^_`abcdefghijklmnopqrstu",
16+
},
17+
{
18+
name: "Z85 (ZeroMQ)",
19+
value: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#",
20+
},
21+
{
22+
name: "IPv6",
23+
value: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|~}",
24+
}
25+
];
26+
27+
28+
/**
29+
* Returns the name of the alphabet, when given the alphabet.
30+
*
31+
* @param {string} alphabet
32+
* @returns {string}
33+
*/
34+
export function alphabetName(alphabet) {
35+
alphabet = alphabet.replace("'", "&apos;");
36+
alphabet = alphabet.replace("\"", "&quot;");
37+
alphabet = alphabet.replace("\\", "&bsol;");
38+
let name;
39+
40+
ALPHABET_OPTIONS.forEach(function(a) {
41+
if (escape(alphabet) === escape(a.value)) name = a.name;
42+
});
43+
44+
return name;
45+
}

src/core/operations/FromBase85.mjs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @author PenguinGeorge [george@penguingeorge.com]
3+
* @copyright Crown Copyright 2018
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation";
8+
import OperationError from "../errors/OperationError";
9+
import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85";
10+
11+
/**
12+
* From Base85 operation
13+
*/
14+
class FromBase85 extends Operation {
15+
16+
/**
17+
* From Base85 constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "From Base85";
23+
this.module = "Default";
24+
this.description = "Base85 (similar to Base64) is a notation for encoding arbitrary byte data. It is usually more efficient that Base64.<br><br>This operation decodes data from an ASCII string (with an alphabet of your choosing, presets included).<br><br>e.g. <code>BOu!rD]j7BEbo7</code> becomes <code>hello world</code><br><br>Base85 is commonly used in Adobe's PostScript and PDF file formats.";
25+
this.infoURL = "https://wikipedia.org/wiki/Ascii85";
26+
this.inputType = "string";
27+
this.outputType = "byteArray";
28+
this.args = [
29+
{
30+
name: "Alphabet",
31+
type: "editableOption",
32+
value: ALPHABET_OPTIONS
33+
},
34+
];
35+
}
36+
37+
/**
38+
* @param {string} input
39+
* @param {Object[]} args
40+
* @returns {byteArray}
41+
*/
42+
run(input, args) {
43+
const alphabet = args[0] || ALPHABET_OPTIONS[0].value,
44+
encoding = alphabetName(alphabet),
45+
result = [];
46+
47+
if (alphabet.length !== 85 ||
48+
[].unique.call(alphabet).length !== 85) {
49+
throw new OperationError("Alphabet must be of length 85");
50+
}
51+
52+
if (input.length === 0) return [];
53+
54+
const matches = input.match(/<~(.+?)~>/);
55+
if (matches !== null) input = matches[1];
56+
57+
let i = 0;
58+
let block, blockBytes;
59+
while (i < input.length) {
60+
if (encoding === "Standard" && input[i] === "z") {
61+
result.push(0, 0, 0, 0);
62+
i++;
63+
} else {
64+
let digits = [];
65+
digits = input
66+
.substr(i, 5)
67+
.split("")
68+
.map((chr, idx) => {
69+
const digit = alphabet.indexOf(chr);
70+
if (digit < 0 || digit > 84) {
71+
throw "Invalid character '" + chr + "' at index " + idx;
72+
}
73+
return digit;
74+
});
75+
76+
block =
77+
digits[0] * 52200625 +
78+
digits[1] * 614125 +
79+
(i + 2 < input.length ? digits[2] : 84) * 7225 +
80+
(i + 3 < input.length ? digits[3] : 84) * 85 +
81+
(i + 4 < input.length ? digits[4] : 84);
82+
83+
blockBytes = [
84+
(block >> 24) & 0xff,
85+
(block >> 16) & 0xff,
86+
(block >> 8) & 0xff,
87+
block & 0xff
88+
];
89+
90+
if (input.length < i + 5) {
91+
blockBytes.splice(input.length - (i + 5), 5);
92+
}
93+
94+
result.push.apply(result, blockBytes);
95+
i += 5;
96+
}
97+
}
98+
99+
return result;
100+
}
101+
102+
}
103+
104+
export default FromBase85;

src/core/operations/ToBase85.mjs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @author PenguinGeorge [george@penguingeorge.com]
3+
* @copyright Crown Copyright 2018
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation";
8+
import OperationError from "../errors/OperationError";
9+
import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85";
10+
11+
/**
12+
* To Base85 operation
13+
*/
14+
class ToBase85 extends Operation {
15+
16+
/**
17+
* To Base85 constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "To Base85";
23+
this.module = "Default";
24+
this.description = "Base85 (similar to Base64) is a notation for encoding arbitrary byte data. It is usually more efficient that Base64.<br><br>This operation encodes data in an ASCII string (with an alphabet of your choosing, presets included).<br><br>e.g. <code>hello world</code> becomes <code>BOu!rD]j7BEbo7</code><br><br>Base85 is commonly used in Adobe's PostScript and PDF file formats.<br><br><strong>Options</strong><br><u>Alphabet</u><ul><li>Standard - The standard alphabet, referred to as Ascii85</li><li>Z85 (ZeroMQ) - A string-safe variant of Base85, which avoids quote marks and backslash characters</li><li>IPv6 - A variant of Base85 suitable for encoding IPv6 addresses (RFC 1924)</li></ul><u>Include delimiter</u><br>Adds a '<~' and '~>' delimiter to the start and end of the data. This is standard for Adobe's implementation of Base85.";
25+
this.infoURL = "https://wikipedia.org/wiki/Ascii85";
26+
this.inputType = "byteArray";
27+
this.outputType = "string";
28+
this.args = [
29+
{
30+
name: "Alphabet",
31+
type: "editableOption",
32+
value: ALPHABET_OPTIONS
33+
},
34+
{
35+
name: "Include Delimeter",
36+
type: "boolean",
37+
value: false
38+
}
39+
];
40+
}
41+
42+
/**
43+
* @param {byteArray} input
44+
* @param {Object[]} args
45+
* @returns {string}
46+
*/
47+
run(input, args) {
48+
const alphabet = args[0] || ALPHABET_OPTIONS[0].value,
49+
encoding = alphabetName(alphabet);
50+
let result = "";
51+
52+
if (alphabet.length !== 85 ||
53+
[].unique.call(alphabet).length !== 85) {
54+
throw new OperationError("Error: alphabet must be of length 85");
55+
}
56+
57+
if (input.length === 0) return "";
58+
59+
let block;
60+
for (let i = 0; i < input.length; i += 4) {
61+
block = (
62+
((input[i]) << 24) +
63+
((input[i + 1] || 0) << 16) +
64+
((input[i + 2] || 0) << 8) +
65+
((input[i + 3] || 0))
66+
) >>> 0;
67+
68+
if (encoding !== "Standard" || block > 0) {
69+
let digits = [];
70+
for (let j = 0; j < 5; j++) {
71+
digits.push(block % 85);
72+
block = Math.floor(block / 85);
73+
}
74+
75+
digits = digits.reverse();
76+
77+
if (input.length < i + 4) {
78+
digits.splice(input.length - (i + 4), 4);
79+
}
80+
81+
result += digits.map(digit => alphabet[digit]).join("");
82+
} else {
83+
result += (encoding === "Standard") ? "z" : null;
84+
}
85+
}
86+
87+
if (args[1] === true) result = "<~" + result + "~>";
88+
89+
return result;
90+
}
91+
}
92+
93+
export default ToBase85;

0 commit comments

Comments
 (0)