|
From: <pe...@us...> - 2004-03-04 22:07:50
|
Update of /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25355/src/test/org/neuclear/commons/crypto Modified Files: Base32Tests.java Log Message: Fixed Base32 encoding. I now use a more elegant understandable approach using BigInteger. It is however 2-3 times slower than Tyler's approach. I'm trying to cut down on the amount of dependencies, and dont want to import another jar for just one method. Index: Base32Tests.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/test/org/neuclear/commons/crypto/Base32Tests.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Base32Tests.java 3 Mar 2004 23:24:25 -0000 1.7 --- Base32Tests.java 4 Mar 2004 21:54:14 -0000 1.8 *************** *** 23,26 **** --- 23,29 ---- $Id$ $Log$ + Revision 1.8 2004/03/04 21:54:14 pelle + Fixed Base32 encoding. I now use a more elegant understandable approach using BigInteger. It is however 2-3 times slower than Tyler's approach. I'm trying to cut down on the amount of dependencies, and dont want to import another jar for just one method. + Revision 1.7 2004/03/03 23:24:25 pelle Added a "test" alias to testkeys.jks *************** *** 51,57 **** /** ! * User: pelleb ! * Date: Jan 16, 2004 ! * Time: 9:52:41 PM */ public class Base32Tests extends TestCase { --- 54,58 ---- /** ! * Tests of Base32 Encode. I'm testing against data produced by Tyler Close http://www.waterken.com's implementation */ public class Base32Tests extends TestCase { *************** *** 83,99 **** 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++){ ! byte[] hash=Base32.encode(CryptoTools.digest(TESTSTRINGS[i])).getBytes(); ! assertTrue("TESTSTRINGS["+i+"]",CryptoTools.equalByteArrays(CryptoTools.digest(TESTSTRINGS[i]),Base32.decode(hash))); } } public void testSHA1HomevsTyler() throws CryptoException { ! for (int i=0;i<TESTSTRINGS.length;i++){ ! assertEquals("TESTSTRINGS["+i+"]",Base32.encode(CryptoTools.digest(TESTSTRINGS[i])),TYLER_SHA1_OUTPUT); } } --- 84,102 ---- 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]))); } } *************** *** 101,107 **** 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); } } --- 104,110 ---- 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); } } *************** *** 109,117 **** 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); --- 112,121 ---- 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); *************** *** 131,134 **** --- 135,173 ---- */ + /** + * 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[]{ "", *************** *** 148,152 **** }; ! static final String TYLER_OUTPUT[]=new String[]{ "", "ga", --- 187,191 ---- }; ! static final String TYLER_OUTPUT[] = new String[]{ "", "ga", *************** *** 165,169 **** }; ! static final String TYLER_SHA1_OUTPUT[]=new String[]{ "3i42h3s6nnfq2msvx7xzkyayscx5qbyj", "wzmj7rvlbxecz4jathi4fvakxgkoqqim", --- 204,208 ---- }; ! static final String TYLER_SHA1_OUTPUT[] = new String[]{ "3i42h3s6nnfq2msvx7xzkyayscx5qbyj", "wzmj7rvlbxecz4jathi4fvakxgkoqqim", |