-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencrypt.js
34 lines (32 loc) · 1005 Bytes
/
encrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* ==========================================================================
*
* Use:
* "Hello World!".encrypt("key")
* => "WzM3NDQyLDUyNDg4LDU1ODc0LDU2MDcyLDU3NDEwLDE3MTYwLDQ1MTIyLDU3NjA4LDU4OTQ2LDU2MDcyLDUxNzc4XQ=="
*
* ========================================================================== */
String.prototype.encode = function() {
let array = [];
for (let i of this) {
array.push(i.charCodeAt(0))
}
return array
}
String.prototype.encrypt = function(key) {
const encoded = this.encode();
const keyEncoded = key.encode();
// console.log(keyEncoded)
let array = encoded.map(x => {
x = parseInt(x)
for (let i of keyEncoded) {
x = x + 1 << i % 8
}
keyEncoded.reverse()
return x;
})
if (typeof btoa === 'undefined') {
global.btoa = str => new Buffer(str, 'binary').toString('base64');
}
return btoa(JSON.stringify(array))
}
module.exports = (text, key) => text.encrypt(key)