minor cleanup, encodefile command in Base64.java
authorbrian <brian@brianweb.net>
Mon, 28 Feb 2005 22:51:27 +0000 (22:51 +0000)
committerbrian <brian@brianweb.net>
Mon, 28 Feb 2005 22:51:27 +0000 (22:51 +0000)
darcs-hash:20050228225127-24bed-efab6fede5a8a6cef16c10eca5696f17da1a5fdd.gz

Makefile
src/org/ibex/crypto/AES.java
src/org/ibex/crypto/Base64.java
src/org/ibex/net/SSL.java
src/org/ibex/net/ssl/SwingVerifyCallback.java

index 68007d8..cf7e69a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -34,11 +34,11 @@ clean:
 
 # This stuff is only for Brian to use 
 # We should probably verify this file somehow
-tmp/.havecacertrs:
+tmp/.havecacerts:
        @mkdir -p tmp
        wget -O - http://ftp.debian.org/debian/pool/main/c/ca-certificates/ca-certificates_20020323.tar.gz | gzip -dc | tar -C tmp -xf-
        cd tmp/ca-certificates/mozilla && \
-               make all \
+               make all; \
                for f in *.pem; do \
                        openssl x509 -in "$$f" -out "$$f.der" -outform der; \
                done
index d1173ca..62ae408 100644 (file)
@@ -99,7 +99,7 @@ public class AES implements Cipher {
         (byte)225, (byte)248, (byte)152,  (byte)17, (byte)105, (byte)217, (byte)142, (byte)148,
         (byte)155,  (byte)30, (byte)135, (byte)233, (byte)206,  (byte)85,  (byte)40, (byte)223,
         (byte)140, (byte)161, (byte)137,  (byte)13, (byte)191, (byte)230,  (byte)66, (byte)104,
-        (byte)65, (byte)153,  (byte)45,  (byte)15, (byte)176,  (byte)84, (byte)187,  (byte)22,
+        (byte)65, (byte)153,  (byte)45,  (byte)15, (byte)176,  (byte)84, (byte)187,  (byte)22
     };
     
     // The inverse S-box
@@ -135,7 +135,7 @@ public class AES implements Cipher {
         (byte)160, (byte)224,  (byte)59,  (byte)77, (byte)174,  (byte)42, (byte)245, (byte)176,
         (byte)200, (byte)235, (byte)187,  (byte)60, (byte)131,  (byte)83, (byte)153,  (byte)97,
         (byte)23,  (byte)43,   (byte)4, (byte)126, (byte)186, (byte)119, (byte)214,  (byte)38,
-        (byte)225, (byte)105,  (byte)20,  (byte)99,  (byte)85,  (byte)33,  (byte)12, (byte)125,
+        (byte)225, (byte)105,  (byte)20,  (byte)99,  (byte)85,  (byte)33,  (byte)12, (byte)125
     };
     
     // vector used in calculating key schedule (powers of x in GF(256))
index 5f66130..04ef163 100644 (file)
@@ -4,12 +4,12 @@
  */
 package org.ibex.crypto;
 
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 
 public final class Base64 {
-       private Base64() { /* can't be instansiated */ }
-       
-    private static final char encodeMap[] = {
+    private Base64() { /* can't be instansiated */ }
+    
+    private static final char[] encodeMap = {
         'A','B','C','D','E','F','G','H',
         'I','J','K','L','M','N','O','P',
         'Q','R','S','T','U','V','W','X',
@@ -20,7 +20,7 @@ public final class Base64 {
         '4','5','6','7','8','9','+','/'
     };
 
-    private static final int decodeMap[] = {
+    private static final int[] decodeMap = {
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
@@ -31,19 +31,27 @@ public final class Base64 {
         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1
     };
 
-       public static void main(String[] args){
+    public static void main(String[] args) throws IOException {
         if(args.length == 2 && args[0].equals("encode"))
-            System.out.println(encode(args[1].getBytes()));
+            System.out.println(encode(getBytes(args[1])));
         else if(args.length == 2 && args[0].equals("decode"))
-            System.out.println(new String(decode(args[1])));
-        else {
-            System.out.println("Usage: Base64 {encode,decode} text");
+            System.out.write(decode(args[1]));
+        else if(args.length == 2 && args[0].equals("encodefile")) {
+            FileInputStream fis = new FileInputStream(args[1]);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            byte[] buf = new byte[4096];
+            int n;
+            while((n = fis.read(buf)) != -1) baos.write(buf,0,n);
+            fis.close();
+            System.out.println(encode(baos.toByteArray()));
+        } else {
+            System.out.println("Usage: Base64 {encode,decode} filename");
             System.exit(1);
         }
         System.exit(0);
-       }
+    }
        
-       public static byte[] decode(String inString){
+    public static byte[] decode(String inString){
         char[] in = inString.toCharArray();
         int part=0;
         int theBytes = 0;
@@ -81,14 +89,15 @@ public final class Base64 {
         }
     }
 
-       public static String encode(String in) { return encode(getBytes(in)); }
+    public static String encode(String in) { return encode(getBytes(in)); }
+    
     public static String encode(byte[] in){
         int part=0;
         int theBytes = 0;
         char[] out = new char[(in.length / 3 + 1) * 4];
         int outPos = 0;
         for(int i=0;i<in.length;i++){
-            theBytes = (theBytes << 8) | in[i];
+            theBytes = (theBytes << 8) | (in[i]&0xff);
             part++;
             if(part == 3){
                 part = 0;
index 39d7f9c..3331de6 100644 (file)
@@ -988,7 +988,7 @@ public class SSL extends Socket {
     }
     
     public static boolean debugOn = false;
-    private static void debug(Object o) { if(debugOn) System.err.println("[IbexSSLL-Debug] " + o.toString()); }
+    private static void debug(Object o) { if(debugOn) System.err.println("[IbexSSL-Debug] " + o.toString()); }
     private static void log(Object o) { System.err.println("[IbexSSL] " + o.toString()); }
             
     private static void verifyCerts(X509.Certificate[] certs) throws DER.Exception, Exn {
index 832ef2f..4374e69 100644 (file)
@@ -69,7 +69,7 @@ public class SwingVerifyCallback extends JDialog implements SSL.VerifyCallback {
         final Object[] messages = new Object[] {
                 "The SSL Certificate the server presented could not be verified.",
                 exn.getMessage(),
-                sp,
+                sp
         };
         Runnable r = new Runnable() { public void run() {
             int n = JOptionPane.showOptionDialog(