|
1
|
|
|
/** |
|
2
|
|
|
* |
|
3
|
|
|
* Base64 encode / decode |
|
4
|
|
|
* @see http://www.webtoolkit.info/ |
|
5
|
|
|
* |
|
6
|
|
|
**/ |
|
7
|
|
|
var Base64 = { |
|
8
|
|
|
// private property |
|
9
|
|
|
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", |
|
10
|
|
|
|
|
11
|
|
|
// public method for encoding |
|
12
|
|
|
encode: function (input) { |
|
13
|
|
|
var output = ""; |
|
14
|
|
|
var chr1, chr2, chr3, enc1, enc2, enc3, enc4; |
|
15
|
|
|
var i = 0; |
|
16
|
|
|
|
|
17
|
|
|
input = Base64._utf8_encode(input); |
|
18
|
|
|
|
|
19
|
|
|
while (i < input.length) { |
|
20
|
|
|
chr1 = input.charCodeAt(i++); |
|
21
|
|
|
chr2 = input.charCodeAt(i++); |
|
22
|
|
|
chr3 = input.charCodeAt(i++); |
|
23
|
|
|
|
|
24
|
|
|
enc1 = chr1 >> 2; |
|
25
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); |
|
26
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); |
|
27
|
|
|
enc4 = chr3 & 63; |
|
28
|
|
|
|
|
29
|
|
|
if (isNaN(chr2)) { |
|
30
|
|
|
enc3 = enc4 = 64; |
|
31
|
|
|
} else if (isNaN(chr3)) { |
|
32
|
|
|
enc4 = 64; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
output = |
|
36
|
|
|
output + |
|
37
|
|
|
this._keyStr.charAt(enc1) + |
|
38
|
|
|
this._keyStr.charAt(enc2) + |
|
39
|
|
|
this._keyStr.charAt(enc3) + |
|
40
|
|
|
this._keyStr.charAt(enc4); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return output; |
|
44
|
|
|
}, |
|
45
|
|
|
|
|
46
|
|
|
// public method for decoding |
|
47
|
|
|
decode: function (input) { |
|
48
|
|
|
var output = ""; |
|
49
|
|
|
var chr1, chr2, chr3; |
|
50
|
|
|
var enc1, enc2, enc3, enc4; |
|
51
|
|
|
var i = 0; |
|
52
|
|
|
|
|
53
|
|
|
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
|
54
|
|
|
|
|
55
|
|
|
while (i < input.length) { |
|
56
|
|
|
enc1 = this._keyStr.indexOf(input.charAt(i++)); |
|
57
|
|
|
enc2 = this._keyStr.indexOf(input.charAt(i++)); |
|
58
|
|
|
enc3 = this._keyStr.indexOf(input.charAt(i++)); |
|
59
|
|
|
enc4 = this._keyStr.indexOf(input.charAt(i++)); |
|
60
|
|
|
|
|
61
|
|
|
chr1 = (enc1 << 2) | (enc2 >> 4); |
|
62
|
|
|
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
|
63
|
|
|
chr3 = ((enc3 & 3) << 6) | enc4; |
|
64
|
|
|
|
|
65
|
|
|
output = output + String.fromCharCode(chr1); |
|
66
|
|
|
|
|
67
|
|
|
if (enc3 != 64) { |
|
68
|
|
|
output = output + String.fromCharCode(chr2); |
|
69
|
|
|
} |
|
70
|
|
|
if (enc4 != 64) { |
|
71
|
|
|
output = output + String.fromCharCode(chr3); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
output = Base64._utf8_decode(output); |
|
76
|
|
|
|
|
77
|
|
|
return output; |
|
78
|
|
|
}, |
|
79
|
|
|
|
|
80
|
|
|
// private method for UTF-8 encoding |
|
81
|
|
|
_utf8_encode: function (string) { |
|
82
|
|
|
string = string.replace(/\r\n/g, "\n"); |
|
83
|
|
|
var utftext = ""; |
|
84
|
|
|
|
|
85
|
|
|
for (var n = 0; n < string.length; n++) { |
|
86
|
|
|
var c = string.charCodeAt(n); |
|
87
|
|
|
|
|
88
|
|
|
if (c < 128) { |
|
89
|
|
|
utftext += String.fromCharCode(c); |
|
90
|
|
|
} else if (c > 127 && c < 2048) { |
|
91
|
|
|
utftext += String.fromCharCode((c >> 6) | 192); |
|
92
|
|
|
utftext += String.fromCharCode((c & 63) | 128); |
|
93
|
|
|
} else { |
|
94
|
|
|
utftext += String.fromCharCode((c >> 12) | 224); |
|
95
|
|
|
utftext += String.fromCharCode(((c >> 6) & 63) | 128); |
|
96
|
|
|
utftext += String.fromCharCode((c & 63) | 128); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return utftext; |
|
101
|
|
|
}, |
|
102
|
|
|
|
|
103
|
|
|
// private method for UTF-8 decoding |
|
104
|
|
|
_utf8_decode: function (utftext) { |
|
105
|
|
|
var string = ""; |
|
106
|
|
|
var i = 0; |
|
107
|
|
|
var c = (c1 = c2 = 0); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
while (i < utftext.length) { |
|
110
|
|
|
c = utftext.charCodeAt(i); |
|
111
|
|
|
|
|
112
|
|
|
if (c < 128) { |
|
113
|
|
|
string += String.fromCharCode(c); |
|
114
|
|
|
i++; |
|
115
|
|
|
} else if (c > 191 && c < 224) { |
|
116
|
|
|
c2 = utftext.charCodeAt(i + 1); |
|
117
|
|
|
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); |
|
118
|
|
|
i += 2; |
|
119
|
|
|
} else { |
|
120
|
|
|
c2 = utftext.charCodeAt(i + 1); |
|
121
|
|
|
c3 = utftext.charCodeAt(i + 2); |
|
|
|
|
|
|
122
|
|
|
string += String.fromCharCode( |
|
123
|
|
|
((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63) |
|
124
|
|
|
); |
|
125
|
|
|
i += 3; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return string; |
|
130
|
|
|
}, |
|
131
|
|
|
}; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* base64 encoding |
|
135
|
|
|
* @param {string} str string raw |
|
136
|
|
|
*/ |
|
137
|
|
|
function base64_encode(str) { |
|
138
|
|
|
// PROCESS |
|
139
|
|
|
//const encodedWord = CryptoJS.enc.Utf8.parse(str); // encodedWord Array object |
|
140
|
|
|
//const encoded = CryptoJS.enc.Base64.stringify(encodedWord); // string: 'NzUzMjI1NDE=' |
|
141
|
|
|
const encoded = Base64.encode(str); |
|
142
|
|
|
return encoded; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Check if base64 is valid |
|
147
|
|
|
* @param {string} str |
|
148
|
|
|
*/ |
|
149
|
|
|
function base64_valid(str) { |
|
150
|
|
|
if (str == "" || str.trim() == "") { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
try { |
|
154
|
|
|
return btoa(atob(str)) == str; |
|
155
|
|
|
} catch (err) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
/** |
|
160
|
|
|
* base64 decoding |
|
161
|
|
|
* @param {string} str base64 string |
|
162
|
|
|
*/ |
|
163
|
|
|
function base64_decode(str) { |
|
164
|
|
|
// PROCESS |
|
165
|
|
|
//const encodedWord = CryptoJS.enc.Base64.parse(str); // encodedWord via Base64.parse() |
|
166
|
|
|
//const decoded = CryptoJS.enc.Utf8.stringify(encodedWord); // decode encodedWord via Utf8.stringify() '75322541' |
|
167
|
|
|
const decoded = Base64.decode(str); |
|
168
|
|
|
return decoded; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
function b64EncodeUnicode(str) { |
|
172
|
|
|
return btoa( |
|
173
|
|
|
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) { |
|
174
|
|
|
return String.fromCharCode(parseInt(p1, 16)); |
|
175
|
|
|
}) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
function b64DecodeUnicode(str) { |
|
180
|
|
|
return decodeURIComponent( |
|
181
|
|
|
Array.prototype.map |
|
182
|
|
|
.call(atob(str), function (c) { |
|
183
|
|
|
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); |
|
184
|
|
|
}) |
|
185
|
|
|
.join("") |
|
186
|
|
|
); |
|
187
|
|
|
} |
|
188
|
|
|
|