/** * 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"); */ }
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 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()); }
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 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 String legalizeUsername(String friendlyUsername) { try { if (!ILLEGAL_USERNAME_PATTERN.matcher(friendlyUsername).find()) return friendlyUsername; MessageDigest digest = MessageDigest.getInstance("SHA1"); digest.update(WeaveUtil.toAsciiBytes(friendlyUsername.toLowerCase())); byte[] baseEncodedBytes = Base32.encode(digest.digest()); return WeaveUtil.toAsciiString(baseEncodedBytes); } catch (GeneralSecurityException e) { throw new Error(e); } }