-
Notifications
You must be signed in to change notification settings - Fork 1
/
base64-encoding.js
43 lines (36 loc) · 1.29 KB
/
base64-encoding.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
35
36
37
38
39
40
41
42
43
String.prototype.toBase64 = function() {
const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const chunks = this.match(/.{1,3}/g);
const parse = (chunk) => {
return chunk.split('').reduce((sum, item, index) => {
return sum + (item.charCodeAt() << (2 - index) * 8);
}, 0)
}
const encode = (pattern) => {
return (
CODES[pattern >> 18 & 0x3F]
+ CODES[pattern >> 12 & 0x3F]
+ CODES[pattern >> 6 & 0x3F || 64]
+ CODES[pattern & 0x3F || 64]
)
}
return chunks.reduce((output, chunk) => output + encode(parse(chunk)), '')
}
String.prototype.fromBase64 = function() {
const CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const chunks = this.match(/.{1,4}/g);
const parse = (chunk) => {
return chunk.split('').reduce((sum, item, index) => {
const code = CODES.indexOf(item);
return (code < 64) ? sum + (code << ((3 - index) * 6)) : sum
}, 0)
}
const decode = (pattern) => {
return (
String.fromCharCode((pattern >> 16) & 0xFF)
+ String.fromCharCode((pattern >> 8) & 0xFF)
+ String.fromCharCode((pattern) & 0xFF)
).replace(/\u0000/g, '');
}
return chunks.reduce((output, chunk) => output + decode(parse(chunk)), '')
}