2019-03-02 17:33:17 +01:00
/**
2019-09-04 17:13:05 +01:00
* @author Karsten Silkenbäumer [github.com/kassi]
* @copyright Karsten Silkenbäumer 2019
* @license Apache-2.0
*/
2019-03-02 17:33:17 +01:00
2019-11-06 06:01:52 -07:00
import Operation from "../Operation.mjs" ;
2019-03-02 17:33:17 +01:00
import {
2019-03-02 17:55:03 +01:00
BACON_ALPHABETS ,
2019-03-02 17:33:17 +01:00
BACON_TRANSLATIONS_FOR_ENCODING , BACON_TRANSLATION_AB ,
swapZeroAndOne
2019-11-06 06:01:52 -07:00
} from "../lib/Bacon.mjs" ;
2019-03-02 17:33:17 +01:00
/**
2019-09-04 17:13:05 +01:00
* Bacon Cipher Encode operation
*/
2019-03-02 17:33:17 +01:00
class BaconCipherEncode extends Operation {
/**
2019-09-04 17:13:05 +01:00
* BaconCipherEncode constructor
*/
2019-03-02 17:33:17 +01:00
constructor () {
super ();
this . name = "Bacon Cipher Encode" ;
this . module = "Default" ;
2019-09-04 17:13:05 +01:00
this . description = "Bacon's cipher or the Baconian cipher is a method of steganography devised by Francis Bacon in 1605. A message is concealed in the presentation of text, rather than its content." ;
this . infoURL = "https://wikipedia.org/wiki/Bacon%27s_cipher" ;
2019-03-02 17:33:17 +01:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Alphabet" ,
"type" : "option" ,
2019-03-02 17:55:03 +01:00
"value" : Object . keys ( BACON_ALPHABETS )
2019-03-02 17:33:17 +01:00
},
{
"name" : "Translation" ,
"type" : "option" ,
"value" : BACON_TRANSLATIONS_FOR_ENCODING
},
{
"name" : "Keep extra characters" ,
"type" : "boolean" ,
"value" : false
},
{
"name" : "Invert Translation" ,
"type" : "boolean" ,
"value" : false
}
];
}
/**
2019-09-04 17:13:05 +01:00
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
2019-03-02 17:33:17 +01:00
run ( input , args ) {
const [ alphabet , translation , keep , invert ] = args ;
2019-03-02 17:55:03 +01:00
const alphabetObject = BACON_ALPHABETS [ alphabet ];
2019-03-02 17:33:17 +01:00
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 ;
2019-03-02 17:55:03 +01:00
if ( alphabetObject . codes !== undefined ) {
code = alphabetObject . codes [ code ];
2019-03-02 17:33:17 +01:00
}
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 ;