blob: 2901d74a14ae6100c0b1182fd9a86aadfd777dd4 (
plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package JACE.Connection;
import JACE.OS.*;
/**
* Collection of methods concerning HTTP.
*/
public class HTTPHelper
{
/**
* Alphabet used in encoding and decoding basic base64 authentication.
* See the HTTP 1.1 RFC for details.
*/
public static String Alphabet
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* Decode a secret encrypted with the basic base64 HTTP 1.1 authentication
* scheme.
*
*@param secret Message to decode
*@return null on error, decoded String on success
*/
public static String DecodeBase64 (String secret)
{
StringBuffer output = new StringBuffer ();
boolean inalphabet [] = new boolean[256];
char decoder [] = new char [256];
for (int i = 0; i < 256; i++) {
inalphabet [i] = false;
decoder [i] = 0;
}
for (int i = Alphabet.length () - 1;
i >= 0;
i--)
{
inalphabet [(int)Alphabet.charAt (i)] = true;
decoder [(int)Alphabet.charAt (i)] = (char)i;
}
int inidx = 0;
int c = 0;
int char_count = 0;
int bits = 0;
boolean error = false;
while (inidx < secret.length ())
{
c = secret.charAt (inidx++);
if (c == '=')
break;
if (c > 255 || !inalphabet[(int)c])
continue;
bits += decoder[c];
char_count++;
if (char_count == 4) {
output.append ((char) (bits >> 16));
output.append ((char) ((bits >> 8) & 0xff));
output.append ((char) (bits & 0xff));
bits = 0;
char_count = 0;
} else
bits <<= 6;
}
if (c == '\0') {
if (char_count != 0) {
ACE.DEBUG ("base64 encoding incomplete: at least "
+ ((4 - char_count) * 6) + " bits truncated");
error = true;
}
} else {
// c == '='
switch (char_count)
{
case 1:
ACE.DEBUG ("output so far: " + output.toString ());
ACE.DEBUG ("base64 encoding incomplete: at least 2 bits missing");
error = true;
break;
case 2:
output.append ((char) (bits >> 10));
break;
case 3:
output.append ((char) (bits >> 16));
output.append ((char) ((bits >> 8) & 0xff));
break;
}
}
if (!error)
return output.toString ();
else
return null;
}
/**
* Encode a message with the basic base64 HTTP 1.1 authentication
* scheme. Adapted from James' JAWS HTTP_Helpers code.
*
*@param secret Message to encode
*@return null on error, an encoded String on success
*/
public static String EncodeBase64 (String secret)
{
StringBuffer output = new StringBuffer ();
// Index of the input string
int inidx = 0;
// character value
int c;
int char_count = 0;
int bits = 0;
boolean error = false;
while (inidx < secret.length())
{
c = secret.charAt(inidx++);
// This will mess up internationalization. I wonder if it is really
// necessary for HTTP?
if (c > 255)
{
ACE.DEBUG ("encountered char > 255 (decimal %d): " + c);
error = true;
break;
}
bits += c;
char_count++;
if (char_count == 3)
{
output.append(HTTPHelper.Alphabet.charAt(bits >> 18));
output.append(HTTPHelper.Alphabet.charAt((bits >> 12) & 0x3f));
output.append(HTTPHelper.Alphabet.charAt((bits >> 6) & 0x3f));
output.append(HTTPHelper.Alphabet.charAt(bits & 0x3f));
bits = 0;
char_count = 0;
}
else
bits <<= 8;
}
if (!error)
{
if (char_count != 0)
{
bits <<= 16 - (8 * char_count);
output.append(HTTPHelper.Alphabet.charAt(bits >> 18));
output.append(HTTPHelper.Alphabet.charAt((bits >> 12) & 0x3f));
if (char_count == 1)
{
output.append("==");
}
else
{
output.append(HTTPHelper.Alphabet.charAt((bits >> 6) & 0x3f));
output.append('=');
}
}
return output.toString();
}
// Returns null on error
return null;
}
private HTTPHelper () {}
}
|