1 7 8 package org.jboss.security; 10 import java.io.ByteArrayInputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.OutputStream ; 15 import java.io.PrintStream ; 16 import java.io.UnsupportedEncodingException ; 17 18 45 public final class Base64Encoder 46 { 47 private static final int BUFFER_SIZE = 1024 ; 48 private static final byte encoding[] = 49 { 50 (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', 51 (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', 53 (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', 55 (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', 57 (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', 59 (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', 61 (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', 63 (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', 65 (byte) '8', (byte) '9', (byte) '+', (byte) '/', (byte) '=' }; 68 69 70 75 public static void encode(InputStream in, OutputStream out) throws IOException 76 { 77 process(in, out); 78 } 79 80 84 public static void encode(byte input[], OutputStream out) throws IOException 85 { 86 ByteArrayInputStream in = new ByteArrayInputStream (input); 87 process(in, out); 88 } 89 90 95 public static String encode(String input) throws IOException 96 { 97 byte bytes[] ; 98 bytes = input.getBytes("ISO-8859-1"); 99 return encode (bytes); 100 } 101 102 105 public static String encode(byte bytes[]) throws IOException 106 { 107 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 108 ByteArrayOutputStream out = new ByteArrayOutputStream (); 109 process(in, out); 110 return out.toString("ISO-8859-1"); 111 } 112 113 124 public static void main (String args[]) throws Exception 125 { 126 if(args.length == 1) 127 { 128 System.out.println ("["+ Base64Encoder.encode(args[0])+"]"); 129 } 132 else if (args.length == 2) 133 { 134 byte[] hash = java.security.MessageDigest.getInstance(args[1]).digest(args[0].getBytes()); 135 System.out.println ("["+ Base64Encoder.encode(hash)+"]"); 136 } 137 else 138 { 139 System.out.println("Usage: Base64Encoder <string> <optional hash algorithm>"); 140 } 141 } 142 143 145 private static int get1(byte buf[], int off) 146 { 147 return (buf[off] & 0xfc) >> 2 ; 148 } 149 150 private static int get2(byte buf[], int off) 151 { 152 return ((buf[off]&0x3) << 4) | ((buf[off+1]&0xf0) >>> 4) ; 153 } 154 155 private static int get3(byte buf[], int off) 156 { 157 return ((buf[off+1] & 0x0f) << 2) | ((buf[off+2] & 0xc0) >>> 6) ; 158 } 159 160 private static int get4(byte buf[], int off) 161 { 162 return buf[off+2] & 0x3f ; 163 } 164 165 172 private static void process(InputStream in, OutputStream out) throws IOException 173 { 174 byte buffer[] = new byte[BUFFER_SIZE] ; 175 int got = -1 ; 176 int off = 0 ; 177 int count = 0 ; 178 while ((got = in.read(buffer, off, BUFFER_SIZE-off)) > 0) 179 { 180 if ( got >= 3 ) 181 { 182 got += off; 183 off = 0; 184 while (off + 3 <= got) 185 { 186 int c1 = get1(buffer,off); 187 int c2 = get2(buffer,off); 188 int c3 = get3(buffer,off); 189 int c4 = get4(buffer,off); 190 switch (count) 191 { 192 case 73: 193 out.write(encoding[c1]); 194 out.write(encoding[c2]); 195 out.write(encoding[c3]); 196 out.write ('\n') ; 197 out.write(encoding[c4]); 198 count = 1 ; 199 break ; 200 case 74: 201 out.write(encoding[c1]); 202 out.write(encoding[c2]); 203 out.write ('\n') ; 204 out.write(encoding[c3]); 205 out.write(encoding[c4]) ; 206 count = 2 ; 207 break ; 208 case 75: 209 out.write(encoding[c1]); 210 out.write ('\n') ; 211 out.write(encoding[c2]); 212 out.write(encoding[c3]); 213 out.write(encoding[c4]) ; 214 count = 3 ; 215 break ; 216 case 76: 217 out.write('\n') ; 218 out.write(encoding[c1]); 219 out.write(encoding[c2]); 220 out.write(encoding[c3]); 221 out.write(encoding[c4]); 222 count = 4; 223 break; 224 default: 225 out.write(encoding[c1]); 226 out.write(encoding[c2]); 227 out.write(encoding[c3]); 228 out.write(encoding[c4]); 229 count += 4; 230 break; 231 } 232 off += 3; 233 } 234 for ( int i = 0 ; i < 3 ;i++) 236 buffer[i] = (i < got-off) ? buffer[off+i] : ((byte) 0); 237 off = got-off ; 238 } 239 else 240 { 241 off += got; 243 } 244 } 245 switch (off) { 247 case 1: 248 out.write(encoding[get1(buffer, 0)]); 249 out.write(encoding[get2(buffer, 0)]); 250 out.write('='); 251 out.write('='); 252 break ; 253 case 2: 254 out.write(encoding[get1(buffer, 0)]); 255 out.write(encoding[get2(buffer, 0)]); 256 out.write(encoding[get3(buffer, 0)]); 257 out.write('='); 258 } 259 return; 260 } 261 } 262 263 | Popular Tags |