A lot of refactoring, and etc #232
1 changed files with 5 additions and 5 deletions
|
@ -1,8 +1,8 @@
|
|||
//converts input base64 into its URL-safe variant
|
||||
let toURLsafe = str => str.replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c))
|
||||
|
||||
//let toURLsafe = str => str.replace(/./gs, c => ({'/': '_', '+': '-'}[c] || c))
|
||||
//https://nodejs.org/docs/latest/api/buffer.html#buffers-and-character-encodings
|
||||
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 toURLsafe(Buffer.from(this.xor(str, key)).toString('base64')) }
|
||||
decrypt(str, key = 37526) { return this.xor(Buffer.from(toURLsafe(str), 'base64').toString(), key) }
|
||||
}
|
||||
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) }
|
||||
}
|
Loading…
Add table
Reference in a new issue