mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
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
This commit is contained in:
@ -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
|
||||
|
18
Ciphers/test/XORCipher.test.js
Normal file
18
Ciphers/test/XORCipher.test.js
Normal file
@ -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')
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user