16
16
import java .io .UnsupportedEncodingException ;
17
17
import java .net .URLDecoder ;
18
18
import java .nio .charset .StandardCharsets ;
19
+ import java .util .Arrays ;
19
20
import java .util .Base64 ;
20
21
21
22
public abstract class Encoding {
22
23
private Encoding () {} /* ensures cannot be constructed */
23
24
25
+ /**
26
+ * base64 encode a byte array to a byte array
27
+ * @param input the input byte array to encode
28
+ * @return the encoded byte array
29
+ */
30
+ public static byte [] base64BasicEncode (byte [] input ) {
31
+ return Base64 .getEncoder ().encode (input );
32
+ }
33
+
34
+ /**
35
+ * base64 encode a byte array to a byte array
36
+ * @param input the input byte array to encode
37
+ * @return the encoded byte array
38
+ */
39
+ public static String base64BasicEncodeToString (byte [] input ) {
40
+ return Base64 .getEncoder ().encodeToString (input );
41
+ }
42
+
24
43
/**
25
44
* base64 url encode a byte array to a byte array
26
45
* @param input the input byte array to encode
27
46
* @return the encoded byte array
28
- * @deprecated prefer base64UrlEncode
29
47
*/
30
- @ Deprecated
31
- public static byte [] base64Encode ( byte [] input ) {
32
- return Base64 . getUrlEncoder (). withoutPadding (). encode ( input );
48
+ public static String base64BasicEncodeToString ( String input ) {
49
+ return Base64 . getEncoder ()
50
+ . encodeToString ( input . getBytes ( StandardCharsets . UTF_8 ) );
33
51
}
34
52
53
+
35
54
/**
36
55
* base64 url encode a byte array to a byte array
37
56
* @param input the input byte array to encode
@@ -42,21 +61,50 @@ public static byte[] base64UrlEncode(byte[] input) {
42
61
}
43
62
44
63
/**
45
- * base64 url encode a byte array to a string
64
+ * base64 url encode a byte array to a byte array
46
65
* @param input the input byte array to encode
47
- * @return the encoded string
66
+ * @return the encoded byte array
48
67
*/
49
- public static String toBase64Url (byte [] input ) {
50
- return new String ( base64UrlEncode ( input ) );
68
+ public static String base64UrlEncodeToString (byte [] input ) {
69
+ return Base64 . getUrlEncoder (). withoutPadding (). encodeToString ( input );
51
70
}
52
71
53
72
/**
54
- * base64 url encode a string to a string
55
- * @param input the input string to encode
56
- * @return the encoded string
73
+ * base64 url encode a byte array to a byte array
74
+ * @param input the input byte array to encode
75
+ * @return the encoded byte array
57
76
*/
58
- public static String toBase64Url (String input ) {
59
- return new String (base64UrlEncode (input .getBytes (StandardCharsets .US_ASCII )));
77
+ public static String base64UrlEncodeToString (String input ) {
78
+ return Base64 .getUrlEncoder ()
79
+ .withoutPadding ()
80
+ .encodeToString (input .getBytes (StandardCharsets .UTF_8 ));
81
+ }
82
+
83
+ /**
84
+ * base64 decode a byte array
85
+ * @param input the input byte array to decode
86
+ * @return the decoded byte array
87
+ */
88
+ public static byte [] base64BasicDecode (byte [] input ) {
89
+ return Base64 .getDecoder ().decode (input );
90
+ }
91
+
92
+ /**
93
+ * base64 decode a base64 encoded string
94
+ * @param input the input string to decode
95
+ * @return the decoded byte array
96
+ */
97
+ public static byte [] base64BasicDecode (String input ) {
98
+ return Base64 .getDecoder ().decode (input );
99
+ }
100
+
101
+ /**
102
+ * base64 decode a base64 encoded string
103
+ * @param input the input string to decode
104
+ * @return the decoded string
105
+ */
106
+ public static String base64BasicDecodeToString (String input ) {
107
+ return new String (Base64 .getDecoder ().decode (input ));
60
108
}
61
109
62
110
/**
@@ -69,12 +117,104 @@ public static byte[] base64UrlDecode(byte[] input) {
69
117
}
70
118
71
119
/**
72
- * get a string from a base64 url encoded byte array
120
+ * base64 url decode a base64 url encoded string
121
+ * @param input the input string to decode
122
+ * @return the decoded byte array
123
+ */
124
+ public static byte [] base64UrlDecode (String input ) {
125
+ return Base64 .getUrlDecoder ().decode (input );
126
+ }
127
+
128
+ /**
129
+ * base64 url decode a base64 url encoded string
73
130
* @param input the input string to decode
74
131
* @return the decoded string
75
132
*/
76
- public static String fromBase64Url (String input ) {
77
- return new String (base64UrlDecode (input .getBytes (StandardCharsets .US_ASCII )));
133
+ public static String base64UrlDecodeToString (String input ) {
134
+ return new String (Base64 .getUrlDecoder ().decode (input ));
135
+ }
136
+
137
+ // http://en.wikipedia.org/wiki/Base_32
138
+ private static final String BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" ;
139
+ private static final int [] BASE32_LOOKUP ;
140
+ private static final int MASK = 31 ;
141
+ private static final int SHIFT = 5 ;
142
+ public static char [] base32Encode (final byte [] input ) {
143
+ int last = input .length ;
144
+ char [] charBuff = new char [(last + 7 ) * 8 / SHIFT ];
145
+ int offset = 0 ;
146
+ int buffer = input [offset ++];
147
+ int bitsLeft = 8 ;
148
+ int i = 0 ;
149
+
150
+ while (bitsLeft > 0 || offset < last ) {
151
+ if (bitsLeft < SHIFT ) {
152
+ if (offset < last ) {
153
+ buffer <<= 8 ;
154
+ buffer |= (input [offset ++] & 0xff );
155
+ bitsLeft += 8 ;
156
+ } else {
157
+ int pad = SHIFT - bitsLeft ;
158
+ buffer <<= pad ;
159
+ bitsLeft += pad ;
160
+ }
161
+ }
162
+ int index = MASK & (buffer >> (bitsLeft - SHIFT ));
163
+ bitsLeft -= SHIFT ;
164
+ charBuff [i ] = BASE32_CHARS .charAt (index );
165
+ i ++;
166
+ }
167
+
168
+ int nonBlank ;
169
+
170
+ for (nonBlank =charBuff .length -1 ;nonBlank >=0 ;nonBlank --) {
171
+ if (charBuff [nonBlank ] != 0 ) {
172
+ break ;
173
+ }
174
+ }
175
+
176
+ char [] retVal = new char [nonBlank +1 ];
177
+
178
+ System .arraycopy (charBuff , 0 , retVal , 0 , retVal .length );
179
+
180
+ Arrays .fill (charBuff , '\0' );
181
+
182
+ return retVal ;
183
+ }
184
+ static {
185
+ BASE32_LOOKUP = new int [256 ];
186
+
187
+ Arrays .fill (BASE32_LOOKUP , 0xFF );
188
+
189
+ for (int i = 0 ; i < BASE32_CHARS .length (); i ++) {
190
+ int index = BASE32_CHARS .charAt (i ) - '0' ;
191
+ BASE32_LOOKUP [index ] = i ;
192
+ }
193
+ }
194
+
195
+ public static byte [] base32Decode (final char [] input ) {
196
+ byte [] bytes = new byte [input .length * SHIFT / 8 ];
197
+ int buffer = 0 ;
198
+ int next = 0 ;
199
+ int bitsLeft = 0 ;
200
+
201
+ for (int i = 0 ; i < input .length ; i ++) {
202
+ int lookup = input [i ] - '0' ;
203
+
204
+ if (lookup < 0 || lookup >= BASE32_LOOKUP .length ) {
205
+ continue ;
206
+ }
207
+
208
+ int c = BASE32_LOOKUP [lookup ];
209
+ buffer <<= SHIFT ;
210
+ buffer |= c & MASK ;
211
+ bitsLeft += SHIFT ;
212
+ if (bitsLeft >= 8 ) {
213
+ bytes [next ++] = (byte ) (buffer >> (bitsLeft - 8 ));
214
+ bitsLeft -= 8 ;
215
+ }
216
+ }
217
+ return bytes ;
78
218
}
79
219
80
220
public static String jsonDecode (String s ) {
@@ -86,7 +226,6 @@ public static String jsonDecode(String s) {
86
226
char nextChar = (x == len - 1 ) ? '\\' : s .charAt (x + 1 );
87
227
switch (nextChar ) {
88
228
case '\\' :
89
- ch = '\\' ;
90
229
break ;
91
230
case 'b' :
92
231
ch = '\b' ;
@@ -178,4 +317,48 @@ public static String uriDecode(String source) {
178
317
return source ;
179
318
}
180
319
}
320
+
321
+ /**
322
+ * @deprecated Use {@link #base64UrlEncode(byte[])} instead.
323
+ * base64 url encode a byte array to a byte array
324
+ * @param input the input byte array to encode
325
+ * @return the encoded byte array
326
+ */
327
+ @ Deprecated
328
+ public static byte [] base64Encode (byte [] input ) {
329
+ return Base64 .getUrlEncoder ().withoutPadding ().encode (input );
330
+ }
331
+
332
+ /**
333
+ * @deprecated Use {@link #base64UrlEncodeToString(byte[])} instead.
334
+ * base64 url encode a byte array to a string
335
+ * @param input the input byte array to encode
336
+ * @return the encoded string
337
+ */
338
+ @ Deprecated
339
+ public static String toBase64Url (byte [] input ) {
340
+ return base64UrlEncodeToString (input );
341
+ }
342
+
343
+ /**
344
+ * @deprecated Use {@link #base64UrlEncodeToString(String)} instead.
345
+ * base64 url encode a string to a string
346
+ * @param input the input string to encode
347
+ * @return the encoded string
348
+ */
349
+ @ Deprecated
350
+ public static String toBase64Url (String input ) {
351
+ return base64UrlEncodeToString (input );
352
+ }
353
+
354
+ /**
355
+ * @deprecated Use {@link #base64UrlDecodeToString(String)} instead.
356
+ * get a string from a base64 url encoded byte array
357
+ * @param input the input string to decode
358
+ * @return the decoded string
359
+ */
360
+ @ Deprecated
361
+ public static String fromBase64Url (String input ) {
362
+ return base64UrlDecodeToString (input );
363
+ }
181
364
}
0 commit comments