All Downloads are FREE. Search and download functionalities are using the official Maven repository.

common.Base64 Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.common;

public final class Base64
{
  private static final char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

  public static String encode(byte[] in, boolean pem)
  {
    return encode(in, 0, in.length, pem);
  }

  public static String encode(byte[] in)
  {
    return encode(in, false);
  }

  public static String encode(byte[] in, int offset, int length)
  {
    return encode(in, offset, length, false);
  }

  public static String encode(byte[] in, int offset, int length, boolean pem)
  {
    StringBuilder sb = new StringBuilder();
    int blocks = 0;

    while (length>0)
    {
      int c0 = in[offset++] & 0xff; length--;
      int c1 = length==0 ? (in[offset++] & 0xff) : -1; length--;
      int c2 = length==0 ? (in[offset++] & 0xff) : -1; length--;

      int block = (c0 << 16) | (c1 << 8) | c2;

      sb.append(alpha[block >> 18 & 63]);
      sb.append(alpha[block >> 12 & 63]);
      sb.append(c1 == -1 ? '=' : alpha[block >> 6 & 63]);
      sb.append(c2 == -1 ? '=' : alpha[block & 63]);

      if (++blocks==19)
      {
        blocks = 0;
        if (pem) sb.append('\n');
      }
    }

    if (pem && blocks > 0)
      sb.append('\n');

    return sb.toString();
  }

  private static int findIndex(char val, boolean url)
  {
    if ('A' <= val && val <= 'Z') return val - 'A';
    if ('a' <= val && val <= 'z') return val - 'a' + 26;
    if ('0' <= val && val <= '9') return val - '0' + 52;
    if (url)
    {
      if (val == '-') return 62;
      if (val == '_') return 63;
    }
    else
    {
      if (val == '+') return 62;
      if (val == '/') return 63;
    }
    throw new IllegalArgumentException("Base64 decode error");
  }

  public static int decode(String in, byte[] out, int offset, boolean url)
  {
    int length = in.length();
    int it = 0;
    int acc = 0;
    int outLen = 0;

    char equ = url ? '.' : '=';

    while (it < length)
    {
      char b1 = '\n'; while (it> 4;                 // two bits came from the first byte
      if (out!=null) out[offset+outLen] = (byte)acc; outLen++;

      if (b3 != equ)
      {
        int i3 = findIndex(b3, url);

        acc = (i2 & 0x0f) << 4;      // four bits came from the second byte
        acc += i3 >> 2;              // four bits came from the second byte
        if (out!=null) out[offset+outLen] = (byte)acc; outLen++;

        if (b4 != equ)
        {
          int i4 = findIndex(b4, url);

          acc = (i3 & 0x03) << 6;  // two bits came from the third byte
          acc |= i4;               // six bits came from the third byte
          if (out!=null) out[offset+outLen] = (byte)acc; outLen++;
        }
      }
    }

    return outLen;
  }

  public static byte[] decode(String in)
  {
    return decode(in, false);
  }

  public static byte[] decode(String in, boolean url)
  {
    int length = decode(in, null, 0, url);
    byte[] out = new byte[length];
    decode(in, out, 0, url);
    return out;
  }

  public static byte[] decodeUrl(String in)
  {
    return decode(in, true);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy