2003/02/12 06:21:05
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:51:16 +0000 (06:51 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:51:16 +0000 (06:51 +0000)
darcs-hash:20040130065116-2ba56-f4005ef8319103543a85eb171f01e69866687c66.gz

src/org/bouncycastle/crypto/io/DigestOutputStream.java [new file with mode: 0644]
src/org/bouncycastle/util/encoders/Base64.java

diff --git a/src/org/bouncycastle/crypto/io/DigestOutputStream.java b/src/org/bouncycastle/crypto/io/DigestOutputStream.java
new file mode 100644 (file)
index 0000000..a2b00a6
--- /dev/null
@@ -0,0 +1,41 @@
+package org.bouncycastle.crypto.io;
+
+import java.io.*;
+
+import org.bouncycastle.crypto.Digest;
+
+public class DigestOutputStream
+    extends FilterOutputStream
+{
+    protected Digest digest;
+
+    public DigestOutputStream(
+        OutputStream    stream,
+        Digest          digest)
+    {
+        super(stream);
+        this.digest = digest;
+    }
+
+    public void write(int b)
+        throws IOException
+    {
+        digest.update((byte)b);
+        out.write(b);
+    }
+
+    public void write(
+        byte[] b,
+        int off,
+        int len)
+        throws IOException
+    {
+        digest.update(b, off, len);
+        out.write(b, off, len);
+    }
+
+    public Digest getDigest()
+    {
+        return digest;
+    }
+}
index 423fb30..448e0ac 100644 (file)
@@ -2,7 +2,7 @@ package org.bouncycastle.util.encoders;
 
 public class Base64
 {
-       private static byte[] encodingTable =
+       private static final byte[] encodingTable =
                {
                    (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
             (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
@@ -27,8 +27,9 @@ public class Base64
                byte[]  data)
        {
                byte[]  bytes;
-
-               if ((data.length % 3) == 0)
+               
+               int modulus = data.length % 3;
+               if (modulus == 0)
                {
                        bytes = new byte[4 * data.length / 3];
                }
@@ -37,25 +38,18 @@ public class Base64
                        bytes = new byte[4 * ((data.length / 3) + 1)];
                }
 
-               for (int i = 0, j = 0;
-                               i < ((data.length / 3) * 3); i += 3, j += 4)
+        int dataLength = (data.length - modulus);
+               int a1, a2, a3;
+               for (int i = 0, j = 0; i < dataLength; i += 3, j += 4)
                {
-                       int     b1, b2, b3, b4;
-                       int     d1, d2, d3;
-
-                       d1 = data[i] & 0xff;
-                       d2 = data[i + 1] & 0xff;
-                       d3 = data[i + 2] & 0xff;
-
-                       b1 = (d1 >>> 2) & 0x3f;
-                       b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
-                       b3 = ((d2 << 2) | (d3 >>> 6)) & 0x3f;
-                       b4 = d3 & 0x3f;
+                       a1 = data[i] & 0xff;
+                       a2 = data[i + 1] & 0xff;
+                       a3 = data[i + 2] & 0xff;
 
-                       bytes[j] = encodingTable[b1];
-                       bytes[j + 1] = encodingTable[b2];
-                       bytes[j + 2] = encodingTable[b3];
-                       bytes[j + 3] = encodingTable[b4];
+                       bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
+                       bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
+                       bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
+                       bytes[j + 3] = encodingTable[a3 & 0x3f];
                }
 
                /*
@@ -64,7 +58,7 @@ public class Base64
                int     b1, b2, b3;
                int     d1, d2;
 
-               switch (data.length % 3)
+               switch (modulus)
                {
                case 0:         /* nothing left to do */
                        break;
@@ -99,7 +93,7 @@ public class Base64
        /*
         * set up the decoding table.
         */
-       private static byte[] decodingTable;
+       private static final byte[] decodingTable;
 
        static
        {