public static boolean equals()

in src/main/java/com/twitter/joauth/Base64Util.java [59:122]


  public static boolean equals(String base64, byte[] bytes) {
    byte[] in = base64.getBytes(UTF_8);
    int length = in.length;
    boolean eof = false;
    int bitWorkArea = 0;
    int modulus = 0;
    int pos = 0;
    int i = 0;
    while (i < length && !eof) {
      byte b = in[i];
      if (b == '=') {
        eof = true;
      } else {
        if (b >= 0 && b < DECODE_TABLE.length) {
          int result = DECODE_TABLE[b];
          if (result >= 0) {
            modulus = (modulus + 1) % BYTES_PER_ENCODED_BLOCK;
            bitWorkArea = (bitWorkArea << BITS_PER_ENCODED_BYTE) + result;
            if (modulus == 0) {
              if (bytes[pos] != (byte)((bitWorkArea >> 16) & 0xff)) {
                return false;
              }
              pos += 1;
              if (bytes[pos] != (byte)((bitWorkArea >> 8) & 0xff)) {
                return false;
              }
              pos += 1;
              if (bytes[pos] != (byte)(bitWorkArea & 0xff)) {
                return false;
              }
              pos += 1;
            }
          }
        }
      }
      i += 1;
    }

    // Some may be left over at the end, we need to compare that as well
    if (eof && modulus != 0) {
      switch(modulus) {
        case 2:
          bitWorkArea = bitWorkArea >> 4;
          if (bytes[pos] != (byte)((bitWorkArea) & 0xff)) {
            return false;
          }
          pos += 1;
        break;
        case 3:
          bitWorkArea = bitWorkArea >> 2;
          if (bytes[pos] != (byte)((bitWorkArea >> 8) & 0xff)) {
            return false;
          }
          pos += 1;
          if (bytes[pos] != (byte)((bitWorkArea) & 0xff)) {
            return false;
          }
          pos += 1;
        break;
      }
    }

    return pos == bytes.length;
  }