Add Bacon cipher encoding

This commit is contained in:
Karsten Silkenbäumer 2019-03-02 17:33:17 +01:00
parent 77b098c5fe
commit a262d70b88
5 changed files with 320 additions and 7 deletions

View file

@ -85,6 +85,7 @@
"Vigenère Decode",
"To Morse Code",
"From Morse Code",
"Bacon Cipher Encode",
"Bacon Cipher Decode",
"Bifid Cipher Encode",
"Bifid Cipher Decode",

View file

@ -12,6 +12,7 @@
export const BACON_ALPHABET_REDUCED = "ABCDEFGHIKLMNOPQRSTUWXYZ";
export const BACON_ALPHABET_COMPLETE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
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];
export const BACON_TRANSLATION_01 = "0/1";
export const BACON_TRANSLATION_AB = "A/B";
export const BACON_TRANSLATION_CASE = "Case";
@ -22,6 +23,10 @@ export const BACON_TRANSLATIONS = [
BACON_TRANSLATION_CASE,
BACON_TRANSLATION_AMNZ,
];
export const BACON_TRANSLATIONS_FOR_ENCODING = [
BACON_TRANSLATION_01,
BACON_TRANSLATION_AB
];
export const BACON_CLEARER_MAP = {
[BACON_TRANSLATIONS[0]]: /[^01]/g,
[BACON_TRANSLATIONS[1]]: /[^ABab]/g,
@ -35,3 +40,22 @@ export const BACON_NORMALIZE_MAP = {
"b": "1"
},
};
/**
* Swaps zeros to ones and ones to zeros.
*
* @param {string} data
* @returns {string}
*
* @example
* // returns "11001 01010"
* swapZeroAndOne("00110 10101");
*/
export function swapZeroAndOne(string) {
return string.replace(/[01]/g, function (c) {
return {
"0": "1",
"1": "0"
}[c];
});
}

View file

@ -9,7 +9,8 @@
import Operation from "../Operation";
import {
BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE,
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
} from "../lib/Bacon";
/**
@ -84,12 +85,7 @@ class BaconCipherDecode extends Operation {
}
if (invert) {
input = input.replace(/./g, function (c) {
return {
"0": "1",
"1": "0"
}[c];
});
input = swapZeroAndOne(input);
}
// group into 5

View file

@ -0,0 +1,103 @@
/**
* BaconCipher operation.
*
* @author kassi [kassi@users.noreply.github.com]
* @copyright Karsten Silkenbäumer 2019
* @license Apache-2.0
*/
import Operation from "../Operation";
import {
BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE,
BACON_TRANSLATIONS_FOR_ENCODING, BACON_TRANSLATION_AB,
swapZeroAndOne
} from "../lib/Bacon";
import { BACON_CODES_REDUCED } from "../lib/Bacon.mjs";
/**
* BaconCipherEncode operation
*/
class BaconCipherEncode extends Operation {
/**
* BaconCipherEncode constructor
*/
constructor() {
super();
this.name = "Bacon Cipher Encode";
this.module = "Default";
this.description = "Bacon's cipher or the Baconian cipher is a method of steganography(a method of hiding a secret message as opposed to just a cipher) devised by Francis Bacon in 1605.[1][2][3] A message is concealed in the presentation of text, rather than its content.";
this.infoURL = "https://en.wikipedia.org/wiki/Bacon%27s_cipher";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Alphabet",
"type": "option",
"value": [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE]
},
{
"name": "Translation",
"type": "option",
"value": BACON_TRANSLATIONS_FOR_ENCODING
},
{
"name": "Keep extra characters",
"type": "boolean",
"value": false
},
{
"name": "Invert Translation",
"type": "boolean",
"value": false
}
];
}
/**
* @param {String} input
* @param {Object[]} args
* @returns {String}
*/
run(input, args) {
const [alphabet, translation, keep, invert] = args;
const charCodeA = "A".charCodeAt(0);
const charCodeZ = "Z".charCodeAt(0);
let output = input.replace(/./g, function (c) {
const charCode = c.toUpperCase().charCodeAt(0);
if (charCode >= charCodeA && charCode <= charCodeZ) {
let code = charCode - charCodeA;
if (alphabet === BACON_ALPHABET_REDUCED) {
code = BACON_CODES_REDUCED[code];
}
const bacon = ("00000" + code.toString(2)).substr(-5, 5);
return bacon;
} else {
return c;
}
});
if (invert) {
output = swapZeroAndOne(output);
}
if (!keep) {
output = output.replace(/[^01]/g, "");
const outputArray = output.match(/(.{5})/g) || [];
output = outputArray.join(" ");
}
if (translation === BACON_TRANSLATION_AB) {
output = output.replace(/[01]/g, function (c) {
return {
"0": "A",
"1": "B"
}[c];
});
}
return output;
}
}
export default BaconCipherEncode;