Skip to content

Commit 862f9d7

Browse files
authored
Refactored XOR.js to call replace once (GDColon#221)
* 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 * Fixed syntax error
1 parent b2320d3 commit 862f9d7

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

classes/XOR.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = class XOR {
22
xor(str, key) { return String.fromCodePoint(...str.split('').map((char, i) => char.charCodeAt(0) ^ key.toString().charCodeAt(i % key.toString().length))) }
3-
encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64').replace(/\//g, '_').replace(/\+/g, '-'); }
4-
decrypt(str, key = 37526) { return this.xor(Buffer.from(str.replace(/\//g, '_').replace(/\+/g, '-'), 'base64').toString(), key) }
5-
}
3+
encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64').replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c)); }
4+
decrypt(str, key = 37526) { return this.xor(Buffer.from(str.replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c)), 'base64').toString(), key) }
5+
}

0 commit comments

Comments
 (0)