--- /dev/null
+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;
+ }
+}
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',
byte[] data)
{
byte[] bytes;
-
- if ((data.length % 3) == 0)
+
+ int modulus = data.length % 3;
+ if (modulus == 0)
{
bytes = new byte[4 * data.length / 3];
}
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];
}
/*
int b1, b2, b3;
int d1, d2;
- switch (data.length % 3)
+ switch (modulus)
{
case 0: /* nothing left to do */
break;
/*
* set up the decoding table.
*/
- private static byte[] decodingTable;
+ private static final byte[] decodingTable;
static
{