Skip to content

Commit c21ad2c

Browse files
authored
merge: Improved xorCipher (TheAlgorithms#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 489544d. * remove: yarn lock * chore: fix grammer * feat: used replace method & added test case * feat: remove revert * chore: added new line
1 parent 075d35f commit c21ad2c

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

Ciphers/XORCipher.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
/**
2+
* @function XORCipher
3+
* @description - Encrypt using an XOR cipher
24
* The XOR cipher is a type of additive cipher.
35
* Each character is bitwise XORed with the key.
46
* We loop through the input string, XORing each
57
* character with the key.
8+
* @param {string} str - string to be encrypted
9+
* @param {number} key - key for encryption
10+
* @return {string} encrypted string
611
*/
7-
8-
/**
9-
* Encrypt using an XOR cipher
10-
* @param {String} str - String to be encrypted
11-
* @param {Number} key - key for encryption
12-
* @return {String} encrypted string
13-
*/
14-
15-
function XOR (str, key) {
16-
let result = ''
17-
for (const elem of str) {
18-
result += String.fromCharCode(elem.charCodeAt(0) ^ key)
12+
const XORCipher = (str, key) => {
13+
if (typeof str !== 'string' || !Number.isInteger(key)) {
14+
throw new TypeError('Arguments type are invalid')
1915
}
20-
return result
21-
}
22-
23-
export { XOR }
2416

25-
// Nb: Node REPL might not output the null char '\x00' (charcode 0)
26-
27-
// > XOR('test string', 32)
28-
// 'TEST\x00STRING'
17+
return str.replace(
18+
/./g, (char) => String.fromCharCode(char.charCodeAt() ^ key)
19+
)
20+
}
2921

30-
// > XOR('TEST\x00STRING', 32)
31-
// 'test string'
22+
export default XORCipher

Ciphers/test/XORCipher.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import XORCipher from '../XORCipher'
2+
3+
describe('Testing XORCipher function', () => {
4+
it('Test - 1, passing a non-string as an argument', () => {
5+
expect(() => XORCipher(false, 0x345)).toThrow()
6+
expect(() => XORCipher(true, 123)).toThrow()
7+
expect(() => XORCipher(1n, 123n)).toThrow()
8+
expect(() => XORCipher(false, 0.34)).toThrow()
9+
expect(() => XORCipher({})).toThrow()
10+
expect(() => XORCipher([])).toThrow()
11+
})
12+
13+
it('Test - 2, passing a string & number as an argument', () => {
14+
// NB: Node REPL might not output the null char '\x00' (charcode 0)
15+
expect(XORCipher('test string', 32)).toBe('TEST\x00STRING')
16+
expect(XORCipher('TEST\x00STRING', 32)).toBe('test string')
17+
})
18+
})

0 commit comments

Comments
 (0)