From 217faa86d6ba4e9e7021231110e598f426453cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Sat, 7 May 2022 19:12:19 -0400 Subject: [PATCH 1/2] Refactored to make 1 call to `replace` Now it's optimized and refactored to replace the same chars as before and leave other chars intact. The `s` (dot-all) flag is used to reduce conditional branching --- classes/XOR.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/XOR.js b/classes/XOR.js index 05e3637..ee56d77 100644 --- a/classes/XOR.js +++ b/classes/XOR.js @@ -1,5 +1,5 @@ module.exports = class XOR { xor(str, key) { return String.fromCodePoint(...str.split('').map((char, i) => char.charCodeAt(0) ^ key.toString().charCodeAt(i % key.toString().length))) } - encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64').replace(/\//g, '_').replace(/\+/g, '-'); } - decrypt(str, key = 37526) { return this.xor(Buffer.from(str.replace(/\//g, '_').replace(/\+/g, '-'), 'base64').toString(), key) } -} \ No newline at end of file + encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64').replace(/./gs, c => {'/': '_', '+': '-'}[c] || c); } + decrypt(str, key = 37526) { return this.xor(Buffer.from(str.replace(/./gs, c => {'/': '_', '+': '-'}[c] || c), 'base64').toString(), key) } +} -- 2.48.1 From a31843e889260cb61ef2f9d19c90d9619e8ad0c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Sat, 7 May 2022 19:23:34 -0400 Subject: [PATCH 2/2] Fixed syntax error --- classes/XOR.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/XOR.js b/classes/XOR.js index ee56d77..87287ca 100644 --- a/classes/XOR.js +++ b/classes/XOR.js @@ -1,5 +1,5 @@ module.exports = class XOR { xor(str, key) { return String.fromCodePoint(...str.split('').map((char, i) => char.charCodeAt(0) ^ key.toString().charCodeAt(i % key.toString().length))) } - encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64').replace(/./gs, c => {'/': '_', '+': '-'}[c] || c); } - decrypt(str, key = 37526) { return this.xor(Buffer.from(str.replace(/./gs, c => {'/': '_', '+': '-'}[c] || c), 'base64').toString(), key) } + encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64').replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c)); } + decrypt(str, key = 37526) { return this.xor(Buffer.from(str.replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c)), 'base64').toString(), key) } } -- 2.48.1