p
This commit is contained in:
parent
e9f84b69b1
commit
e2f7bcbb47
1 changed files with 13 additions and 5 deletions
|
@ -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) }
|
||||
}
|
Loading…
Add table
Reference in a new issue