public void testSHA1vsDecodedTyler() throws CryptoException {
   for (int i = 0; i < TESTSTRINGS.length; i++) {
     assertTrue(
         "TESTSTRINGS[" + i + "]",
         CryptoTools.equalByteArrays(
             CryptoTools.digest(TESTSTRINGS[i]), Base32.decode(TYLER_SHA1_OUTPUT[i])));
   }
 }
 public void testSHA1vsDecodedOwn() throws CryptoException {
   for (int i = 0; i < TESTSTRINGS.length; i++) {
     String hash = Base32.encode(CryptoTools.digest(TESTSTRINGS[i]));
     assertEquals(
         "TESTSTRINGS[" + i + "]",
         new String(CryptoTools.digest(TESTSTRINGS[i])),
         new String(Base32.decode(hash)));
   }
 }
 public void testSHA1HomevsTyler() throws CryptoException {
   for (int i = 0; i < TESTSTRINGS.length; i++) {
     assertEquals(
         "TESTSTRINGS[" + i + "]",
         TYLER_SHA1_OUTPUT[i],
         Base32.encode(CryptoTools.digest(TESTSTRINGS[i])));
   }
 }
 public void testLength() throws CryptoException {
   assertEquals(32, Base32.encode(CryptoTools.digest("hello")).length());
   assertEquals(8, Base32.encode("hello").length());
   assertEquals(10, Base32.encode("hello1").length());
   assertEquals(12, Base32.encode("hello12").length());
   assertEquals(13, Base32.encode("hello123").length());
   assertEquals(15, Base32.encode("hello1234").length());
   assertEquals(16, Base32.encode("hello12345").length());
   assertEquals(18, Base32.encode("hello123456").length());
 }
/**
 * Tests of Base32 Encode. I'm testing against data produced by Tyler Close
 * http://www.waterken.com's implementation
 */
public class Base32Tests extends TestCase {
  public Base32Tests(String string) {
    super(string);
  }

  public void testLength() throws CryptoException {
    assertEquals(32, Base32.encode(CryptoTools.digest("hello")).length());
    assertEquals(8, Base32.encode("hello").length());
    assertEquals(10, Base32.encode("hello1").length());
    assertEquals(12, Base32.encode("hello12").length());
    assertEquals(13, Base32.encode("hello123").length());
    assertEquals(15, Base32.encode("hello1234").length());
    assertEquals(16, Base32.encode("hello12345").length());
    assertEquals(18, Base32.encode("hello123456").length());
  }

  public void testBase32Codec() throws CryptoException {

    for (int i = 0; i < TESTSTRINGS.length; i++) {
      //            System.out.print("Encoding: "+TESTSTRINGS[i]+" ...");
      final String encoded = Base32.encode(TESTSTRINGS[i]);
      System.out.println(" ->" + encoded);
      assertEquals("TESTSTRINGS[" + i + "]", TESTSTRINGS[i].getBytes(), Base32.decode(encoded));
    }
  }

  public void testSHA1vsDecodedTyler() throws CryptoException {
    for (int i = 0; i < TESTSTRINGS.length; i++) {
      assertTrue(
          "TESTSTRINGS[" + i + "]",
          CryptoTools.equalByteArrays(
              CryptoTools.digest(TESTSTRINGS[i]), Base32.decode(TYLER_SHA1_OUTPUT[i])));
    }
  }

  public void testSHA1vsDecodedOwn() throws CryptoException {
    for (int i = 0; i < TESTSTRINGS.length; i++) {
      String hash = Base32.encode(CryptoTools.digest(TESTSTRINGS[i]));
      assertEquals(
          "TESTSTRINGS[" + i + "]",
          new String(CryptoTools.digest(TESTSTRINGS[i])),
          new String(Base32.decode(hash)));
    }
  }

  public void testSHA1HomevsTyler() throws CryptoException {
    for (int i = 0; i < TESTSTRINGS.length; i++) {
      assertEquals(
          "TESTSTRINGS[" + i + "]",
          TYLER_SHA1_OUTPUT[i],
          Base32.encode(CryptoTools.digest(TESTSTRINGS[i])));
    }
  }

  public void testBase32vsTyler() throws CryptoException {

    for (int i = 0; i < TESTSTRINGS.length; i++) {
      final String encoded = Base32.encode(TESTSTRINGS[i]);
      assertEquals("TESTSTRINGS[" + i + "]", TYLER_OUTPUT[i], encoded);
    }
  }

  public void testDecodeTyler() throws CryptoException {

    for (int i = 0; i < TESTSTRINGS.length; i++) {
      final byte decoded[] = Base32.decode(TYLER_OUTPUT[i]);
      assertEquals("TESTSTRINGS[" + i + "]", TESTSTRINGS[i].getBytes(), decoded);
    }
  }

  public void assertEquals(String description, byte a[], byte b[]) {
    assertEquals(description + " length", a.length, b.length);
    for (int i = 0; i < a.length; i++) assertEquals(description + "[" + i + "]", a[i], b[i]);
  }

  /*
      public void testOutputTyler() throws CryptoException{
          for (int i=0;i<TESTSTRINGS.length;i++){
              final String encoded = com.waterken.url.Base32.encode(CryptoTools.digest(TESTSTRINGS[i]));
              System.out.println("\""+encoded+"\",");
          }
      }
  */

  /**
   * Silly Microbenchmark
   *
   * @throws CryptoException
   */
  public void testBenchmark() throws CryptoException {
    System.out.println("BigInteger encoding benchmarks:");
    final int ITERATIONS = 100000;
    final Runtime runtime = Runtime.getRuntime();
    long start = System.currentTimeMillis();
    long memstart = runtime.freeMemory();
    for (int i = 0; i < ITERATIONS; i++) {
      final String encoded = Base32.encode(TESTSTRINGS[i % TESTSTRINGS.length]);
      assertEquals("TESTSTRINGS[" + i + "]", TYLER_OUTPUT[i % TESTSTRINGS.length], encoded);
    }
    long dur = System.currentTimeMillis() - start;
    long memuse = memstart - runtime.freeMemory();
    System.out.println(ITERATIONS + " iterations took: " + dur + "ms");
    System.out.println(ITERATIONS + " iterations used: " + memuse + " bytes");
    /*
            System.out.println("\nWaterken encoding benchmarks:");
            start=System.currentTimeMillis();
            memstart=runtime.freeMemory();
            for (int i=0;i<ITERATIONS;i++){
                final String encoded = com.waterken.url.Base32.encode(TESTSTRINGS[i%TESTSTRINGS.length].getBytes());
                assertEquals("TESTSTRINGS["+i+"]",TYLER_OUTPUT[i%TESTSTRINGS.length],encoded);
            }
            dur=System.currentTimeMillis()-start;
            memuse=memstart-runtime.freeMemory();
            System.out.println(ITERATIONS+" iterations took: "+dur+"ms");
            System.out.println(ITERATIONS+" iterations used: "+memuse+" bytes");
    */

  }

  static final String TESTSTRINGS[] =
      new String[] {
        "",
        "0",
        "01",
        "012",
        "0123",
        "01234",
        "012345",
        "0123456",
        "01234567",
        "012345678",
        "0123456789",
        "0123456789A",
        "0123456789A0123456789As0123456789A",
        new String(CryptoTools.digest("0123456"))
      };
  static final String TYLER_OUTPUT[] =
      new String[] {
        "",
        "ga",
        "gayq",
        "gayte",
        "gaytemy",
        "gaytemzu",
        "gaytemzugu",
        "gaytemzugu3a",
        "gaytemzugu3do",
        "gaytemzugu3dooa",
        "gaytemzugu3doobz",
        "gaytemzugu3doobzie",
        "gaytemzugu3doobzieydcmrtgq2tmnzyhfaxgmbrgiztinjwg44dsqi",
        "h47qqpz7h4cd6hb7cq7t6pz7ha7wwx3x"
      };

  static final String TYLER_SHA1_OUTPUT[] =
      new String[] {
        "3i42h3s6nnfq2msvx7xzkyayscx5qbyj",
        "wzmj7rvlbxecz4jathi4fvakxgkoqqim",
        "3x7bmm2f2m4bsowcxxayh6hj3t7zas2d",
        "ysrntg6crurwbgfasutxw7vqoggwxydi",
        "ys24q26vo7nd3e76u7ejzotby6furzmj",
        "cgieutulo73cilrnfcdqkar23liaveyq",
        "7x4lywauknxwmajiqtqunkeipjchbgsw",
        "w7wqramqyiclghgxcsconiofhcmgwx3x",
        "zsvi3dompubqzvvgo2g3qh4q2dxzo3b5",
        "tjyutjnhpbv3g2hanuemlv3xotvuhje6",
        "q6woyf6ntxgsbjywzqwpm5axw4oiu4aw",
        "weh5oeb3kibeinn6i3jiaskxum76y7dn",
        "uo4jb3ds2i2bb2qbds2h5nusuwum2fxh",
        "5bhw3daqpo2iq5eqbqgt54g5otdjldov"
      };
}