/* * Copyright (c) 2004 World Wide Web Consortium, * * (Massachusetts Institute of Technology, European Research Consortium for * Informatics and Mathematics, Keio University). All Rights Reserved. This * work is distributed under the W3C(r) Software License [1] in the hope that * it will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 */ package org.jboss.security; // for the time being ... import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * BASE64 encoder implementation. * Provides encoding methods, using the BASE64 encoding rules, as defined * in the MIME specification, rfc1521. * * * This class is a modified version based on code * obtained from the w3 consortium website, * which is subject to their generic copyright notice: * *
* Alternatively, use the openssl utility, for example: *
* echo -n "password" | openssl dgst -sha1 -binary | openssl base64 * */ public static void main (String args[]) throws Exception { if(args.length == 1) { System.out.println ("["+ Base64Encoder.encode(args[0])+"]"); // joe:eoj -> am9lOmVvag== // 12345678:87654321 -> MTIzNDU2Nzg6ODc2NTQzMjE= } else if (args.length == 2) { byte[] hash = java.security.MessageDigest.getInstance(args[1]).digest(args[0].getBytes()); System.out.println ("["+ Base64Encoder.encode(hash)+"]"); } else { System.out.println(PicketBoxMessages.MESSAGES.base64EncoderMessage()); } } // Private ---------------------------------------------------------------- private static int get1(byte buf[], int off) { return (buf[off] & 0xfc) >> 2 ; } private static int get2(byte buf[], int off) { return ((buf[off]&0x3) << 4) | ((buf[off+1]&0xf0) >>> 4) ; } private static int get3(byte buf[], int off) { return ((buf[off+1] & 0x0f) << 2) | ((buf[off+2] & 0xc0) >>> 6) ; } private static int get4(byte buf[], int off) { return buf[off+2] & 0x3f ; } /** * Process the data: encode the input stream to the output stream. * This method runs through the input stream, encoding it to the output * stream. * @exception IOException If we weren't able to access the input stream or * the output stream. */ private static void process(InputStream in, OutputStream out) throws IOException { byte buffer[] = new byte[BUFFER_SIZE] ; int got = -1 ; int off = 0 ; int count = 0 ; while ((got = in.read(buffer, off, BUFFER_SIZE-off)) > 0) { if ( got >= 3 ) { got += off; off = 0; while (off + 3 <= got) { int c1 = get1(buffer,off); int c2 = get2(buffer,off); int c3 = get3(buffer,off); int c4 = get4(buffer,off); switch (count) { case 73: out.write(encoding[c1]); out.write(encoding[c2]); out.write(encoding[c3]); out.write ('\n') ; out.write(encoding[c4]); count = 1 ; break ; case 74: out.write(encoding[c1]); out.write(encoding[c2]); out.write ('\n') ; out.write(encoding[c3]); out.write(encoding[c4]) ; count = 2 ; break ; case 75: out.write(encoding[c1]); out.write ('\n') ; out.write(encoding[c2]); out.write(encoding[c3]); out.write(encoding[c4]) ; count = 3 ; break ; case 76: out.write('\n') ; out.write(encoding[c1]); out.write(encoding[c2]); out.write(encoding[c3]); out.write(encoding[c4]); count = 4; break; default: out.write(encoding[c1]); out.write(encoding[c2]); out.write(encoding[c3]); out.write(encoding[c4]); count += 4; break; } off += 3; } // Copy remaining bytes to beginning of buffer: for ( int i = 0 ; i < 3 ;i++) buffer[i] = (i < got-off) ? buffer[off+i] : ((byte) 0); off = got-off ; } else { // Total read amount is less then 3 bytes: off += got; } } // Manage the last bytes, from 0 to off: switch (off) { case 1: out.write(encoding[get1(buffer, 0)]); out.write(encoding[get2(buffer, 0)]); out.write('='); out.write('='); break ; case 2: out.write(encoding[get1(buffer, 0)]); out.write(encoding[get2(buffer, 0)]); out.write(encoding[get3(buffer, 0)]); out.write('='); } return; } }