mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 07:16:17 -04:00
Use better names for the alphabet selection
This commit is contained in:
parent
a262d70b88
commit
d36cede0c7
4 changed files with 21 additions and 15 deletions
|
@ -9,10 +9,15 @@
|
||||||
/**
|
/**
|
||||||
* Bacon definitions.
|
* Bacon definitions.
|
||||||
*/
|
*/
|
||||||
|
export const BACON_ALPHABETS = {
|
||||||
export const BACON_ALPHABET_REDUCED = "ABCDEFGHIKLMNOPQRSTUWXYZ";
|
"Standard (I=J and U=V)": {
|
||||||
export const BACON_ALPHABET_COMPLETE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
alphabet: "ABCDEFGHIKLMNOPQRSTUWXYZ",
|
||||||
export const BACON_CODES_REDUCED = [0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23];
|
codes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23]
|
||||||
|
},
|
||||||
|
"Complete": {
|
||||||
|
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
}
|
||||||
|
};
|
||||||
export const BACON_TRANSLATION_01 = "0/1";
|
export const BACON_TRANSLATION_01 = "0/1";
|
||||||
export const BACON_TRANSLATION_AB = "A/B";
|
export const BACON_TRANSLATION_AB = "A/B";
|
||||||
export const BACON_TRANSLATION_CASE = "Case";
|
export const BACON_TRANSLATION_CASE = "Case";
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import {
|
import {
|
||||||
BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE,
|
BACON_ALPHABETS,
|
||||||
BACON_TRANSLATION_CASE, BACON_TRANSLATION_AMNZ, BACON_TRANSLATIONS, BACON_CLEARER_MAP, BACON_NORMALIZE_MAP,
|
BACON_TRANSLATION_CASE, BACON_TRANSLATION_AMNZ, BACON_TRANSLATIONS, BACON_CLEARER_MAP, BACON_NORMALIZE_MAP,
|
||||||
swapZeroAndOne
|
swapZeroAndOne
|
||||||
} from "../lib/Bacon";
|
} from "../lib/Bacon";
|
||||||
|
@ -33,7 +33,7 @@ class BaconCipherDecode extends Operation {
|
||||||
{
|
{
|
||||||
"name": "Alphabet",
|
"name": "Alphabet",
|
||||||
"type": "option",
|
"type": "option",
|
||||||
"value": [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE]
|
"value": Object.keys(BACON_ALPHABETS)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Translation",
|
"name": "Translation",
|
||||||
|
@ -55,10 +55,11 @@ class BaconCipherDecode extends Operation {
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const [alphabet, translation, invert] = args;
|
const [alphabet, translation, invert] = args;
|
||||||
// split text into groups of 5 characters
|
const alphabetObject = BACON_ALPHABETS[alphabet];
|
||||||
|
|
||||||
// remove invalid characters
|
// remove invalid characters
|
||||||
input = input.replace(BACON_CLEARER_MAP[translation], "");
|
input = input.replace(BACON_CLEARER_MAP[translation], "");
|
||||||
|
|
||||||
// normalize to unique alphabet
|
// normalize to unique alphabet
|
||||||
if (BACON_NORMALIZE_MAP[translation] !== undefined) {
|
if (BACON_NORMALIZE_MAP[translation] !== undefined) {
|
||||||
input = input.replace(/./g, function (c) {
|
input = input.replace(/./g, function (c) {
|
||||||
|
@ -95,7 +96,7 @@ class BaconCipherDecode extends Operation {
|
||||||
for (let index = 0; index < inputArray.length; index++) {
|
for (let index = 0; index < inputArray.length; index++) {
|
||||||
const code = inputArray[index];
|
const code = inputArray[index];
|
||||||
const number = parseInt(code, 2);
|
const number = parseInt(code, 2);
|
||||||
output += number < alphabet.length ? alphabet[number] : "?";
|
output += number < alphabetObject.alphabet.length ? alphabetObject.alphabet[number] : "?";
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,10 @@
|
||||||
|
|
||||||
import Operation from "../Operation";
|
import Operation from "../Operation";
|
||||||
import {
|
import {
|
||||||
BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE,
|
BACON_ALPHABETS,
|
||||||
BACON_TRANSLATIONS_FOR_ENCODING, BACON_TRANSLATION_AB,
|
BACON_TRANSLATIONS_FOR_ENCODING, BACON_TRANSLATION_AB,
|
||||||
swapZeroAndOne
|
swapZeroAndOne
|
||||||
} from "../lib/Bacon";
|
} from "../lib/Bacon";
|
||||||
import { BACON_CODES_REDUCED } from "../lib/Bacon.mjs";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BaconCipherEncode operation
|
* BaconCipherEncode operation
|
||||||
|
@ -34,7 +33,7 @@ class BaconCipherEncode extends Operation {
|
||||||
{
|
{
|
||||||
"name": "Alphabet",
|
"name": "Alphabet",
|
||||||
"type": "option",
|
"type": "option",
|
||||||
"value": [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE]
|
"value": Object.keys(BACON_ALPHABETS)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Translation",
|
"name": "Translation",
|
||||||
|
@ -62,6 +61,7 @@ class BaconCipherEncode extends Operation {
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const [alphabet, translation, keep, invert] = args;
|
const [alphabet, translation, keep, invert] = args;
|
||||||
|
|
||||||
|
const alphabetObject = BACON_ALPHABETS[alphabet];
|
||||||
const charCodeA = "A".charCodeAt(0);
|
const charCodeA = "A".charCodeAt(0);
|
||||||
const charCodeZ = "Z".charCodeAt(0);
|
const charCodeZ = "Z".charCodeAt(0);
|
||||||
|
|
||||||
|
@ -69,8 +69,8 @@ class BaconCipherEncode extends Operation {
|
||||||
const charCode = c.toUpperCase().charCodeAt(0);
|
const charCode = c.toUpperCase().charCodeAt(0);
|
||||||
if (charCode >= charCodeA && charCode <= charCodeZ) {
|
if (charCode >= charCodeA && charCode <= charCodeZ) {
|
||||||
let code = charCode - charCodeA;
|
let code = charCode - charCodeA;
|
||||||
if (alphabet === BACON_ALPHABET_REDUCED) {
|
if (alphabetObject.codes !== undefined) {
|
||||||
code = BACON_CODES_REDUCED[code];
|
code = alphabetObject.codes[code];
|
||||||
}
|
}
|
||||||
const bacon = ("00000" + code.toString(2)).substr(-5, 5);
|
const bacon = ("00000" + code.toString(2)).substr(-5, 5);
|
||||||
return bacon;
|
return bacon;
|
||||||
|
|
|
@ -6,9 +6,9 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
import TestRegister from "../TestRegister";
|
import TestRegister from "../TestRegister";
|
||||||
import { BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE, BACON_TRANSLATIONS } from "../../../src/core/lib/Bacon";
|
import { BACON_ALPHABETS, BACON_TRANSLATIONS } from "../../../src/core/lib/Bacon";
|
||||||
|
|
||||||
const alphabets = [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE];
|
const alphabets = Object.keys(BACON_ALPHABETS);
|
||||||
const translations = BACON_TRANSLATIONS;
|
const translations = BACON_TRANSLATIONS;
|
||||||
|
|
||||||
TestRegister.addTests([
|
TestRegister.addTests([
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue