Skip to content

Commit

Permalink
simplify the code by using a ternary operator to choose between decry…
Browse files Browse the repository at this point in the history
…ption and encryption of the text
  • Loading branch information
sergeiown committed Jul 2, 2024
1 parent 41f68e4 commit d039472
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
13 changes: 3 additions & 10 deletions base64FromFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ const rl = readline.createInterface({
// Function for decoding base64 and encoding to base64 text
function processText() {
rl.on('line', (text) => {
const isEncoded = isBase64(text);
const result = isBase64(text) ? decodeText(text) : Buffer.from(text, 'utf8').toString('base64');

if (isEncoded) {
const decodedText = decodeText(text);
console.log('Decrypted text:', `${decodedText}`);
writeToOutputFile(`${decodedText}${os.EOL}`);
} else {
const encodedText = Buffer.from(text, 'utf8').toString('base64');
console.log('Encrypted text:', `${encodedText}`);
writeToOutputFile(`${encodedText}${os.EOL}`);
}
console.log(isBase64(text) ? 'Decrypted text:' : 'Encrypted text:', `${result}`);
writeToOutputFile(`${result}${os.EOL}`);
});

rl.on('close', () => {
Expand Down
12 changes: 3 additions & 9 deletions xorFromFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ const encryptionKey = 12345;
function processText() {
rl.on('line', (text) => {
const isEncoded = isXOREncoded(text);
const result = isEncoded ? decodeText(text, encryptionKey) : encodeText(text, encryptionKey);

if (isEncoded) {
const decodedText = decodeText(text, encryptionKey);
console.log('Decrypted text:', `${decodedText}`);
writeToOutputFile(`${decodedText}${os.EOL}`);
} else {
const encodedText = encodeText(text, encryptionKey);
console.log('Encrypted text:', `${encodedText}`);
writeToOutputFile(`${encodedText}${os.EOL}`);
}
console.log(isEncoded ? 'Decrypted text:' : 'Encrypted text:', result);
writeToOutputFile(`${result}${os.EOL}`);
});

rl.on('close', () => {
Expand Down

0 comments on commit d039472

Please sign in to comment.