This commit is contained in:
Ricardo Fernández Serrata 2022-06-14 14:16:00 -04:00 committed by GitHub
parent e9f84b69b1
commit e2f7bcbb47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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) }
}