1 package org.neuclear.commons.crypto;
2
3 import junit.framework.TestCase;
4
5 /*
6 NeuClear Distributed Transaction Clearing Platform
7 (C) 2003 Pelle Braendgaard
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 $Id: Base32Tests.java,v 1.8 2004/03/04 21:54:14 pelle Exp $
24 $Log: Base32Tests.java,v $
25 Revision 1.8 2004/03/04 21:54:14 pelle
26 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.
27
28 Revision 1.7 2004/03/03 23:24:25 pelle
29 Added a "test" alias to testkeys.jks
30
31 Revision 1.6 2004/02/19 15:29:13 pelle
32 Various cleanups and corrections
33
34 Revision 1.5 2004/02/18 00:13:42 pelle
35 Many, many clean ups. I've readded Targets in a new method.
36 Gotten rid of NamedObjectBuilder and revamped Identity and Resolvers
37
38 Revision 1.4 2004/01/19 23:49:29 pelle
39 Unit testing uncovered further issues with Base32
40 NSTools is now uptodate as are many other classes. All transactional builders habe been updated.
41 Well on the way towards full "green" on Junit.
42
43 Revision 1.3 2004/01/19 17:53:14 pelle
44 Various clean ups
45
46 Revision 1.2 2004/01/18 21:20:20 pelle
47 Created Base32 encoder that now fully complies with Tyler's spec.
48
49 Revision 1.1 2004/01/16 23:41:59 pelle
50 Added Base32 class. The Base32 encoding used wasnt following the standards.
51 Added user creatable Identity for Public Keys
52
53 */
54
55 /***
56 * Tests of Base32 Encode. I'm testing against data produced by Tyler Close http://www.waterken.com's implementation
57 */
58 public class Base32Tests extends TestCase {
59 public Base32Tests(String string) {
60 super(string);
61 }
62
63 public void testLength() throws CryptoException {
64 assertEquals(32, Base32.encode(CryptoTools.digest("hello")).length());
65 assertEquals(8, Base32.encode("hello").length());
66 assertEquals(10, Base32.encode("hello1").length());
67 assertEquals(12, Base32.encode("hello12").length());
68 assertEquals(13, Base32.encode("hello123").length());
69 assertEquals(15, Base32.encode("hello1234").length());
70 assertEquals(16, Base32.encode("hello12345").length());
71 assertEquals(18, Base32.encode("hello123456").length());
72
73 }
74
75 public void testBase32Codec() throws CryptoException {
76
77 for (int i = 0; i < TESTSTRINGS.length; i++) {
78 // System.out.print("Encoding: "+TESTSTRINGS[i]+" ...");
79 final String encoded = Base32.encode(TESTSTRINGS[i]);
80 System.out.println(" ->" + encoded);
81 assertEquals("TESTSTRINGS[" + i + "]", TESTSTRINGS[i].getBytes(), Base32.decode(encoded));
82 }
83 }
84
85 public void testSHA1vsDecodedTyler() throws CryptoException {
86 for (int i = 0; i < TESTSTRINGS.length; i++) {
87 assertTrue("TESTSTRINGS[" + i + "]", CryptoTools.equalByteArrays(CryptoTools.digest(TESTSTRINGS[i]), Base32.decode(TYLER_SHA1_OUTPUT[i])));
88 }
89 }
90
91 public void testSHA1vsDecodedOwn() throws CryptoException {
92 for (int i = 0; i < TESTSTRINGS.length; i++) {
93 String hash = Base32.encode(CryptoTools.digest(TESTSTRINGS[i]));
94 assertEquals("TESTSTRINGS[" + i + "]", new String(CryptoTools.digest(TESTSTRINGS[i])), new String(Base32.decode(hash)));
95 }
96 }
97
98 public void testSHA1HomevsTyler() throws CryptoException {
99 for (int i = 0; i < TESTSTRINGS.length; i++) {
100 assertEquals("TESTSTRINGS[" + i + "]", TYLER_SHA1_OUTPUT[i], Base32.encode(CryptoTools.digest(TESTSTRINGS[i])));
101 }
102 }
103
104 public void testBase32vsTyler() throws CryptoException {
105
106 for (int i = 0; i < TESTSTRINGS.length; i++) {
107 final String encoded = Base32.encode(TESTSTRINGS[i]);
108 assertEquals("TESTSTRINGS[" + i + "]", TYLER_OUTPUT[i], encoded);
109 }
110 }
111
112 public void testDecodeTyler() throws CryptoException {
113
114 for (int i = 0; i < TESTSTRINGS.length; i++) {
115 final byte decoded[] = Base32.decode(TYLER_OUTPUT[i]);
116 assertEquals("TESTSTRINGS[" + i + "]", TESTSTRINGS[i].getBytes(), decoded);
117 }
118 }
119
120 public void assertEquals(String description, byte a[], byte b[]) {
121 assertEquals(description + " length", a.length, b.length);
122 for (int i = 0; i < a.length; i++)
123 assertEquals(description + "[" + i + "]", a[i], b[i]);
124
125 }
126
127
128 /*
129 public void testOutputTyler() throws CryptoException{
130 for (int i=0;i<TESTSTRINGS.length;i++){
131 final String encoded = com.waterken.url.Base32.encode(CryptoTools.digest(TESTSTRINGS[i]));
132 System.out.println("\""+encoded+"\",");
133 }
134 }
135 */
136
137 /***
138 * Silly Microbenchmark
139 *
140 * @throws CryptoException
141 */
142 public void testBenchmark() throws CryptoException {
143 System.out.println("BigInteger encoding benchmarks:");
144 final int ITERATIONS = 100000;
145 final Runtime runtime = Runtime.getRuntime();
146 long start = System.currentTimeMillis();
147 long memstart = runtime.freeMemory();
148 for (int i = 0; i < ITERATIONS; i++) {
149 final String encoded = Base32.encode(TESTSTRINGS[i % TESTSTRINGS.length]);
150 assertEquals("TESTSTRINGS[" + i + "]", TYLER_OUTPUT[i % TESTSTRINGS.length], encoded);
151 }
152 long dur = System.currentTimeMillis() - start;
153 long memuse = memstart - runtime.freeMemory();
154 System.out.println(ITERATIONS + " iterations took: " + dur + "ms");
155 System.out.println(ITERATIONS + " iterations used: " + memuse + " bytes");
156 /*
157 System.out.println("\nWaterken encoding benchmarks:");
158 start=System.currentTimeMillis();
159 memstart=runtime.freeMemory();
160 for (int i=0;i<ITERATIONS;i++){
161 final String encoded = com.waterken.url.Base32.encode(TESTSTRINGS[i%TESTSTRINGS.length].getBytes());
162 assertEquals("TESTSTRINGS["+i+"]",TYLER_OUTPUT[i%TESTSTRINGS.length],encoded);
163 }
164 dur=System.currentTimeMillis()-start;
165 memuse=memstart-runtime.freeMemory();
166 System.out.println(ITERATIONS+" iterations took: "+dur+"ms");
167 System.out.println(ITERATIONS+" iterations used: "+memuse+" bytes");
168 */
169
170 }
171
172 static final String TESTSTRINGS[] = new String[]{
173 "",
174 "0",
175 "01",
176 "012",
177 "0123",
178 "01234",
179 "012345",
180 "0123456",
181 "01234567",
182 "012345678",
183 "0123456789",
184 "0123456789A",
185 "0123456789A0123456789As0123456789A",
186 new String(CryptoTools.digest("0123456"))
187
188 };
189 static final String TYLER_OUTPUT[] = new String[]{
190 "",
191 "ga",
192 "gayq",
193 "gayte",
194 "gaytemy",
195 "gaytemzu",
196 "gaytemzugu",
197 "gaytemzugu3a",
198 "gaytemzugu3do",
199 "gaytemzugu3dooa",
200 "gaytemzugu3doobz",
201 "gaytemzugu3doobzie",
202 "gaytemzugu3doobzieydcmrtgq2tmnzyhfaxgmbrgiztinjwg44dsqi",
203 "h47qqpz7h4cd6hb7cq7t6pz7ha7wwx3x"
204 };
205
206 static final String TYLER_SHA1_OUTPUT[] = new String[]{
207 "3i42h3s6nnfq2msvx7xzkyayscx5qbyj",
208 "wzmj7rvlbxecz4jathi4fvakxgkoqqim",
209 "3x7bmm2f2m4bsowcxxayh6hj3t7zas2d",
210 "ysrntg6crurwbgfasutxw7vqoggwxydi",
211 "ys24q26vo7nd3e76u7ejzotby6furzmj",
212 "cgieutulo73cilrnfcdqkar23liaveyq",
213 "7x4lywauknxwmajiqtqunkeipjchbgsw",
214 "w7wqramqyiclghgxcsconiofhcmgwx3x",
215 "zsvi3dompubqzvvgo2g3qh4q2dxzo3b5",
216 "tjyutjnhpbv3g2hanuemlv3xotvuhje6",
217 "q6woyf6ntxgsbjywzqwpm5axw4oiu4aw",
218 "weh5oeb3kibeinn6i3jiaskxum76y7dn",
219 "uo4jb3ds2i2bb2qbds2h5nusuwum2fxh",
220 "5bhw3daqpo2iq5eqbqgt54g5otdjldov"
221 };
222 }
This page was automatically generated by Maven