This commit is contained in:
Ricardo Fernández Serrata 2022-06-14 13:35:15 -04:00 committed by GitHub
parent 6eb5accbf4
commit cf7313c7d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,8 @@
//converts input base64 into its URL-safe variant
let toURLsafe = str => str.replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c))
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) }
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 toURLsafe(Buffer.from(this.xor(str, key)).toString('base64')) }
decrypt(str, key = 37526) { return this.xor(Buffer.from(toURLsafe(str), 'base64').toString(), key) }
}