-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'base45' of https://github.com/t-8ch/CyberChef into t-8c…
…h-base45
- Loading branch information
Showing
7 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Base45 resources. | ||
* | ||
* @author Thomas Weißschuh [thomas@t-8ch.de] | ||
* @copyright Crown Copyright 2021 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Highlight to Base45 | ||
*/ | ||
export function highlightToBase45(pos, args) { | ||
pos[0].start = Math.floor(pos[0].start / 2) * 3; | ||
pos[0].end = Math.ceil(pos[0].end / 2) * 3; | ||
return pos; | ||
} | ||
|
||
/** | ||
* Highlight from Base45 | ||
*/ | ||
export function highlightFromBase45(pos, args) { | ||
pos[0].start = Math.floor(pos[0].start / 3) * 2; | ||
pos[0].end = Math.ceil(pos[0].end / 3) * 2; | ||
return pos; | ||
} | ||
|
||
export const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @author Thomas Weißschuh [thomas@t-8ch.de] | ||
* @copyright Crown Copyright 2021 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs"; | ||
import Operation from "../Operation.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
import Utils from "../Utils.mjs"; | ||
|
||
|
||
/** | ||
* From Base45 operation | ||
*/ | ||
class FromBase45 extends Operation { | ||
|
||
/** | ||
* FromBase45 constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "From Base45"; | ||
this.module = "Default"; | ||
this.description = "Base45 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers. The high number base results in shorter strings than with the decimal or hexadecimal system. Base45 is optimized for usage with QR codes."; | ||
this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems"; | ||
this.inputType = "string"; | ||
this.outputType = "byteArray"; | ||
|
||
this.highlight = highlightFromBase45; | ||
this.highlightReverse = highlightToBase45; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {byteArray} | ||
*/ | ||
run(input, args) { | ||
if (!input) return []; | ||
|
||
const res = []; | ||
|
||
for (const triple of Utils.chunked(input, 3)) { | ||
triple.reverse(); | ||
let b = 0; | ||
for (const c of triple) { | ||
const idx = ALPHABET.indexOf(c); | ||
if (idx === -1) { | ||
throw new OperationError(`Character not in alphabet: '${c}'`); | ||
} | ||
b *= 45; | ||
b += idx; | ||
} | ||
|
||
if (b > 65535) { | ||
throw new OperationError(`Triplet too large: '${triple.join("")}'`); | ||
} | ||
|
||
if (triple.length > 2) { | ||
/** | ||
* The last triple may only have 2 bytes so we push the MSB when we got 3 bytes | ||
* Pushing MSB | ||
*/ | ||
res.push(b >> 8); | ||
} | ||
|
||
/** | ||
* Pushing LSB | ||
*/ | ||
res.push(b & 0xff); | ||
|
||
} | ||
|
||
return res; | ||
} | ||
|
||
} | ||
|
||
export default FromBase45; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @author Thomas Weißschuh [thomas@t-8ch.de] | ||
* @copyright Crown Copyright 2021 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs"; | ||
import Operation from "../Operation.mjs"; | ||
import Utils from "../Utils.mjs"; | ||
|
||
/** | ||
* To Base45 operation | ||
*/ | ||
class ToBase45 extends Operation { | ||
|
||
/** | ||
* ToBase45 constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "To Base45"; | ||
this.module = "Default"; | ||
this.description = "Base45 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers. The high number base results in shorter strings than with the decimal or hexadecimal system. Base45 is optimized for usage with QR codes."; | ||
this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems"; | ||
this.inputType = "ArrayBuffer"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
name: "Alphabet", | ||
type: "string", | ||
value: "0-9A-Za-z" | ||
} | ||
]; | ||
|
||
this.highlight = highlightToBase45; | ||
this.highlightReverse = highlightFromBase45; | ||
} | ||
|
||
/** | ||
* @param {ArrayBuffer} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
run(input, args) { | ||
input = new Uint8Array(input); | ||
if (!input) return ""; | ||
|
||
const res = []; | ||
|
||
for (const pair of Utils.chunked(input, 2)) { | ||
let b = 0; | ||
for (const e of pair) { | ||
b *= 256; | ||
b += e; | ||
} | ||
|
||
let chars = 0; | ||
do { | ||
res.push(ALPHABET[b % 45]); | ||
chars++; | ||
b = Math.floor(b / 45); | ||
} while (b > 0); | ||
|
||
if (chars < 2) { | ||
res.push("0"); | ||
} | ||
} | ||
|
||
|
||
return res.join(""); | ||
|
||
} | ||
|
||
} | ||
|
||
export default ToBase45; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Base45 tests. | ||
* | ||
* @author Thomas Weißschuh [thomas@t-8ch.de] | ||
* | ||
* @copyright Crown Copyright 2021 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "To Base45: nothing", | ||
input: "", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "To Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "To Base45: Spec encoding example 1", | ||
input: "AB", | ||
expectedOutput: "BB8", | ||
recipeConfig: [ | ||
{ | ||
op: "To Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "To Base45: Spec encoding example 2", | ||
input: "Hello!!", | ||
expectedOutput: "%69 VD92EX0", | ||
recipeConfig: [ | ||
{ | ||
op: "To Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "To Base45: Spec encoding example 3", | ||
input: "base-45", | ||
expectedOutput: "UJCLQE7W581", | ||
recipeConfig: [ | ||
{ | ||
op: "To Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "From Base45: nothing", | ||
input: "", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "From Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "From Base45: Spec decoding example 1", | ||
input: "QED8WEX0", | ||
expectedOutput: "ietf!", | ||
recipeConfig: [ | ||
{ | ||
op: "From Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "From Base45: Invalid character", | ||
input: "!", | ||
expectedOutput: "Character not in alphabet: '!'", | ||
recipeConfig: [ | ||
{ | ||
op: "From Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "From Base45: Invalid triplet value", | ||
input: "ZZZ", | ||
expectedOutput: "Triplet too large: 'ZZZ'", | ||
recipeConfig: [ | ||
{ | ||
op: "From Base45", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
]); |