-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base64.dart
84 lines (65 loc) · 2.23 KB
/
Base64.dart
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#library('base64');
class Base64 {
String _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
String encode(String input) {
String output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
//input = this.utf8Encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
if (i < input.length) {
chr2 = input.charCodeAt(i++);
} else {
chr2 = null;
}
if (i < input.length) {
chr3 = input.charCodeAt(i++);
} else {
chr3 = null;
}
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (chr2.isNaN()) {
enc3 = enc4 = 64;
} else if (chr3.isNaN()) {
enc4 = 64;
}
output = output +
this._keyStr[enc1] + this._keyStr[enc2] +
this._keyStr[enc3] + this._keyStr[enc4];
}
/*
if (input.length % 3 == 1) {
output = output.substring(0, output.length - 2);
}
if (input.length % 3 == 2) {
output = output.substring(0, output.length - 3);
}
*/
return output;
}
String utf8Encode(String string) {
// private method for UTF-8 encoding
string = string.replaceAll('/\r\n/g',"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += new String.fromCharCodes([c]); //String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += new String.fromCharCodes([(c >> 6) | 192]); //String.fromCharCode((c >> 6) | 192);
utftext += new String.fromCharCodes([(c & 63) | 128]);//String.fromCharCode((c & 63) | 128);
}
else {
utftext += new String.fromCharCodes([(c >> 12) | 224]); //String.fromCharCode((c >> 12) | 224);
utftext += new String.fromCharCodes([((c >> 6) & 63) | 128]); //String.fromCharCode(((c >> 6) & 63) | 128);
utftext += new String.fromCharCodes([(c & 63) | 128]); //String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
}