Merge branch 'SamueleFacendaSubstitution' of https://github.com/SamueleFacenda/CyberChef

This commit is contained in:
n1474335 2022-11-25 11:21:04 +00:00
commit 743b834f6d
1 changed files with 50 additions and 8 deletions

View File

@ -34,10 +34,49 @@ class Substitute extends Operation {
"name": "Ciphertext",
"type": "binaryString",
"value": "XYZABCDEFGHIJKLMNOPQRSTUVW"
},
{
"name": "Ignore case",
"type": "boolean",
"value": false
}
];
}
/**
* 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;
}
/**
* @param {string} input
* @param {Object[]} args
@ -46,17 +85,20 @@ class Substitute extends Operation {
run(input, args) {
const plaintext = Utils.expandAlphRange([...args[0]]),
ciphertext = Utils.expandAlphRange([...args[1]]);
let output = "",
index = -1;
let output = "";
const ignoreCase = args[2];
if (plaintext.length !== ciphertext.length) {
if (plaintext.length !== ciphertext.length)
output = "Warning: Plaintext and Ciphertext lengths differ\n\n";
}
for (const character of input) {
index = plaintext.indexOf(character);
output += index > -1 && index < ciphertext.length ? ciphertext[index] : character;
}
// 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);
return output;
}