From 12d1ca620364f4f8f478f0006f21b8c982f697fe Mon Sep 17 00:00:00 2001 From: brian Date: Mon, 28 Feb 2005 22:51:27 +0000 Subject: [PATCH] minor cleanup, encodefile command in Base64.java darcs-hash:20050228225127-24bed-efab6fede5a8a6cef16c10eca5696f17da1a5fdd.gz --- Makefile | 4 +-- src/org/ibex/crypto/AES.java | 4 +-- src/org/ibex/crypto/Base64.java | 37 +++++++++++++++---------- src/org/ibex/net/SSL.java | 2 +- src/org/ibex/net/ssl/SwingVerifyCallback.java | 2 +- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 68007d8..cf7e69a 100644 --- 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 diff --git a/src/org/ibex/crypto/AES.java b/src/org/ibex/crypto/AES.java index d1173ca..62ae408 100644 --- a/src/org/ibex/crypto/AES.java +++ b/src/org/ibex/crypto/AES.java @@ -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)) diff --git a/src/org/ibex/crypto/Base64.java b/src/org/ibex/crypto/Base64.java index 5f66130..04ef163 100644 --- a/src/org/ibex/crypto/Base64.java +++ b/src/org/ibex/crypto/Base64.java @@ -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