From c21ad2ca7a19396231d3af64a0e458628142e2de Mon Sep 17 00:00:00 2001 From: Fahim Faisaal <57553028+fahimfaisaal@users.noreply.github.com> Date: Tue, 29 Mar 2022 22:33:26 +0600 Subject: [PATCH] merge: Improved `xorCipher` (#965) * feat: improved memoize function used Map instead of object & used the JSON.stringfy method for generate a valid string as a key * docs: modified documentation * style: format with standard * docs: modified stringify doc * refactor: remove two repetition implementation * feat: added validation, test codes * chore: remove useless words * feat: added types for jest * chore: added link box * feat: added new validation test casses & methods * style: formated with standard * feat: added parse method & test cases * docs: added js docs * chore: added default import export * feat: imporved algorithm via replace method * test: added two test cases * feat: added jest type for suggestions * feat: added `reduceRight` & `trim` method * chore: added helper variable * feat: added new rotation option * Revert "chore: added helper variable" This reverts commit 489544da0a3d479910fbea020d3be3d0d10681bf. * remove: yarn lock * chore: fix grammer * feat: used replace method & added test case * feat: remove revert * chore: added new line --- Ciphers/XORCipher.js | 35 +++++++++++++--------------------- Ciphers/test/XORCipher.test.js | 18 +++++++++++++++++ 2 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 Ciphers/test/XORCipher.test.js diff --git a/Ciphers/XORCipher.js b/Ciphers/XORCipher.js index 944081b99..d73827a2e 100644 --- a/Ciphers/XORCipher.js +++ b/Ciphers/XORCipher.js @@ -1,31 +1,22 @@ /** + * @function XORCipher + * @description - Encrypt using an XOR cipher * The XOR cipher is a type of additive cipher. * Each character is bitwise XORed with the key. * We loop through the input string, XORing each * character with the key. + * @param {string} str - string to be encrypted + * @param {number} key - key for encryption + * @return {string} encrypted string */ - -/** - * Encrypt using an XOR cipher - * @param {String} str - String to be encrypted - * @param {Number} key - key for encryption - * @return {String} encrypted string - */ - -function XOR (str, key) { - let result = '' - for (const elem of str) { - result += String.fromCharCode(elem.charCodeAt(0) ^ key) +const XORCipher = (str, key) => { + if (typeof str !== 'string' || !Number.isInteger(key)) { + throw new TypeError('Arguments type are invalid') } - return result + + return str.replace( + /./g, (char) => String.fromCharCode(char.charCodeAt() ^ key) + ) } -export { XOR } - -// Nb: Node REPL might not output the null char '\x00' (charcode 0) - -// > XOR('test string', 32) -// 'TEST\x00STRING' - -// > XOR('TEST\x00STRING', 32) -// 'test string' +export default XORCipher diff --git a/Ciphers/test/XORCipher.test.js b/Ciphers/test/XORCipher.test.js new file mode 100644 index 000000000..9697fc715 --- /dev/null +++ b/Ciphers/test/XORCipher.test.js @@ -0,0 +1,18 @@ +import XORCipher from '../XORCipher' + +describe('Testing XORCipher function', () => { + it('Test - 1, passing a non-string as an argument', () => { + expect(() => XORCipher(false, 0x345)).toThrow() + expect(() => XORCipher(true, 123)).toThrow() + expect(() => XORCipher(1n, 123n)).toThrow() + expect(() => XORCipher(false, 0.34)).toThrow() + expect(() => XORCipher({})).toThrow() + expect(() => XORCipher([])).toThrow() + }) + + it('Test - 2, passing a string & number as an argument', () => { + // NB: Node REPL might not output the null char '\x00' (charcode 0) + expect(XORCipher('test string', 32)).toBe('TEST\x00STRING') + expect(XORCipher('TEST\x00STRING', 32)).toBe('test string') + }) +})