2018-05-11 17:32:19 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
import Utils from "../Utils.mjs" ;
2018-05-11 17:32:19 +02:00
/ * *
* Substitute operation
* /
class Substitute extends Operation {
/ * *
* Substitute constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Substitute" ;
2018-08-21 20:07:13 +02:00
this . module = "Default" ;
2018-11-19 15:54:37 +01:00
this . description = "A substitution cipher allowing you to specify bytes to replace with other byte values. This can be used to create Caesar ciphers but is more powerful as any byte value can be substituted, not just letters, and the substitution values need not be in order.<br><br>Enter the bytes you want to replace in the Plaintext field and the bytes to replace them with in the Ciphertext field.<br><br>Non-printable bytes can be specified using string escape notation. For example, a line feed character can be written as either <code>\\n</code> or <code>\\x0a</code>.<br><br>Byte ranges can be specified using a hyphen. For example, the sequence <code>0123456789</code> can be written as <code>0-9</code>.<br><br>Note that blackslash characters are used to escape special characters, so will need to be escaped themselves if you want to use them on their own (e.g.<code>\\\\</code>)." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Substitution_cipher" ;
2018-05-11 17:32:19 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Plaintext" ,
"type" : "binaryString" ,
"value" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
} ,
{
"name" : "Ciphertext" ,
"type" : "binaryString" ,
"value" : "XYZABCDEFGHIJKLMNOPQRSTUVW"
2022-11-13 14:37:19 +01:00
} ,
{
"name" : "Ignore case" ,
"type" : "boolean" ,
"value" : false
2018-05-11 17:32:19 +02:00
}
] ;
}
2022-11-13 14:37:19 +01:00
/ * *
* Convert a single character using the dictionary , if ignoreCase is true then
* check in the dictionary for both upper and lower case versions of the character .
* In output the input character case is preserved .
* @ param { string } char
* @ param { Object } dict
* @ param { boolean } ignoreCase
* @ returns { string }
* /
cipherSingleChar ( char , dict , ignoreCase ) {
if ( ! ignoreCase )
return dict [ char ] || char ;
const isUpperCase = char === char . toUpperCase ( ) ;
// convert using the dictionary keeping the case of the input character
if ( dict [ char ] !== undefined )
// if the character is in the dictionary return the value with the input case
return isUpperCase ? dict [ char ] . toUpperCase ( ) : dict [ char ] . toLowerCase ( ) ;
// check for the other case, if it is in the dictionary return the value with the right case
if ( isUpperCase ) {
if ( dict [ char . toLowerCase ( ) ] !== undefined )
return dict [ char . toLowerCase ( ) ] . toUpperCase ( ) ;
} else {
if ( dict [ char . toUpperCase ( ) ] !== undefined )
return dict [ char . toUpperCase ( ) ] . toLowerCase ( ) ;
}
return char ;
}
2018-05-11 17:32:19 +02:00
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2022-05-13 17:35:50 +02:00
const plaintext = Utils . expandAlphRange ( [ ... args [ 0 ] ] ) ,
ciphertext = Utils . expandAlphRange ( [ ... args [ 1 ] ] ) ;
2022-11-13 14:37:19 +01:00
let output = "" ;
const ignoreCase = args [ 2 ] ;
2018-05-11 17:32:19 +02:00
2022-11-13 14:37:19 +01:00
if ( plaintext . length !== ciphertext . length )
2018-05-11 17:32:19 +02:00
output = "Warning: Plaintext and Ciphertext lengths differ\n\n" ;
2022-11-13 14:37:19 +01:00
// create dictionary for conversion
const dict = { } ;
for ( let i = 0 ; i < Math . min ( ciphertext . length , plaintext . length ) ; i ++ )
dict [ plaintext [ i ] ] = ciphertext [ i ] ;
// map every letter with the conversion function
for ( const character of input )
output += this . cipherSingleChar ( character , dict , ignoreCase ) ;
2018-05-11 17:32:19 +02:00
return output ;
}
}
export default Substitute ;