From 3a853bf1c1acc4923610c3a9724f81f8c9b2a7fd Mon Sep 17 00:00:00 2001 From: Ankur-Kataria Date: Tue, 1 Sep 2026 19:44:52 +0530 Subject: [PATCH 1/2] fix(ciphers): XORCipher now encrypts newline characters (closes #308) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous implementation used str.replace(/./g, ...) where the dot metacharacter does not match line-terminator characters (\n, \r, etc.) without the s (dotAll) flag, so those bytes were silently passed through unencrypted — an information leak and a correctness bug. Replace the regex-based replacement with Array.from(), which iterates over every character regardless of whether it is a line terminator. The cipher logic (XOR with key) and the return type are unchanged. Add two new test cases: - encrypts \n and \r correctly (previously left unchanged) - XORCipher is its own inverse, including strings with newlines --- ciphers/test/xor_cipher.test.ts | 13 +++++++++++++ ciphers/xor_cipher.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ciphers/test/xor_cipher.test.ts b/ciphers/test/xor_cipher.test.ts index 0607d6a3..38db6c14 100644 --- a/ciphers/test/xor_cipher.test.ts +++ b/ciphers/test/xor_cipher.test.ts @@ -5,4 +5,17 @@ describe('Testing XORCipher function', () => { expect(XORCipher('test', 32)).toBe('TEST') expect(XORCipher('TEST', 32)).toBe('test') }) + + it('encrypts newline and other line-terminator characters', () => { + // /./g without the s-flag skips \n, \r — Array.from handles them correctly + expect(XORCipher('\n', 1)).toBe(String.fromCharCode(10 ^ 1)) + expect(XORCipher('\r', 1)).toBe(String.fromCharCode(13 ^ 1)) + expect(XORCipher('A\nB', 1)).toBe( + String.fromCharCode(65 ^ 1) + String.fromCharCode(10 ^ 1) + String.fromCharCode(66 ^ 1) + ) + }) + + it('XORCipher is its own inverse', () => { + expect(XORCipher(XORCipher('Hello\nWorld', 42), 42)).toBe('Hello\nWorld') + }) }) diff --git a/ciphers/xor_cipher.ts b/ciphers/xor_cipher.ts index 72d91e68..d6930191 100644 --- a/ciphers/xor_cipher.ts +++ b/ciphers/xor_cipher.ts @@ -10,6 +10,6 @@ * @return {string} encrypted string */ export const XORCipher = (str: string, key: number): string => - str.replace(/./g, (char: string) => + Array.from(str, (char: string) => String.fromCharCode(char.charCodeAt(0) ^ key) - ) + ).join('') From db91103b3f39c7d28d12bd1b23d054e6391680a0 Mon Sep 17 00:00:00 2001 From: Ankur-Kataria Date: Tue, 1 Sep 2026 19:45:27 +0530 Subject: [PATCH 2/2] fix(ciphers): XORCipher now encrypts newline characters --- .oss-upstream | 1 + 1 file changed, 1 insertion(+) create mode 100644 .oss-upstream diff --git a/.oss-upstream b/.oss-upstream new file mode 100644 index 00000000..4e304ffa --- /dev/null +++ b/.oss-upstream @@ -0,0 +1 @@ +TheAlgorithms/TypeScript