From e2f7bcbb4720f1b5ba1f29f2345c55deaadb676d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Tue, 14 Jun 2022 14:16:00 -0400 Subject: [PATCH] p --- classes/XOR.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/classes/XOR.js b/classes/XOR.js index 692f536..51bbb4e 100644 --- a/classes/XOR.js +++ b/classes/XOR.js @@ -1,8 +1,16 @@ -//converts input base64 into its URL-safe variant -//let toURLsafe = str => str.replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c)) //https://nodejs.org/docs/latest/api/buffer.html#buffers-and-character-encodings +//both only work on "binary strings" and "URI-safe B64" +let toB64 = str => Buffer.from(str).toString('base64url') +let fromB64 = str => Buffer.from(str, 'base64').toString() + +const defKey = 37526 + module.exports = class XOR { - xor(str, key) { return String.fromCodePoint(...str.split('').map((c, i) => c.charCodeAt(0) ^ key.toString().charCodeAt(i % key.toString().length))) } - encrypt(str, key = 37526) { return Buffer.from(this.xor(str, key)).toString('base64url') } - decrypt(str, key = 37526) { return this.xor(Buffer.from(str, 'base64').toString(), key) } + xor(str, key) { + key = key.toString() + return String.fromCodePoint(...str.split('') + .map((c, i) => c.charCodeAt(0) ^ key.charCodeAt(i % key.length))) + } + encrypt(str, key = defKey) { return toB64(this.xor(str, key)) } + decrypt(str, key = defKey) { return this.xor(fromB64(str), key) } } \ No newline at end of file