1 /*
2 $Id: CryptoToolsTest.java,v 1.6 2004/09/07 21:16:48 pelle Exp $
3 $Log: CryptoToolsTest.java,v $
4 Revision 1.6 2004/09/07 21:16:48 pelle
5 Gor rid of some of the benchmark code in the unit tests. There really is no need for a half an hour benchmark to run everytime I run the tests.
6 Also got rid of some of the annoying messages in AlwaysTheSamePassphraseAgent
7
8 Revision 1.5 2004/08/26 01:54:10 pelle
9 Updated the CryptoToolsTest to better see performance
10
11 Revision 1.4 2004/08/26 01:06:37 pelle
12 Fixed a bug in CryptoTools with regards to seeding the srng
13
14 Revision 1.3 2004/04/09 20:02:55 pelle
15 Added PrivateKey wrapping and unwrapping to CryptoTools with the methods:
16 byte [] wrapKey(char passphrase[], PrivateKey key)
17 and
18 PrivateKey unWrapKey(char passphrase[],byte wrapped[],String algorithm)
19 PrivateKey unWrapRSAKey(char passphrase[],byte wrapped[])
20
21 Revision 1.2 2003/11/21 04:43:42 pelle
22 EncryptedFileStore now works. It uses the PBECipher with DES3 afair.
23 Otherwise You will Finaliate.
24 Anything that can be final has been made final throughout everyting. We've used IDEA's Inspector tool to find all instance of variables that could be final.
25 This should hopefully make everything more stable (and secure).
26
27 Revision 1.1 2003/11/11 21:17:52 pelle
28 Further vital reshuffling.
29 org.neudist.crypto.* and org.neudist.utils.* have been moved to respective areas under org.neuclear.commons
30 org.neuclear.signers.* as well as org.neuclear.passphraseagents have been moved under org.neuclear.commons.crypto as well.
31 Did a bit of work on the Canonicalizer and changed a few other minor bits.
32
33 Revision 1.5 2003/11/09 03:27:09 pelle
34 More house keeping and shuffling about mainly pay
35
36 Revision 1.4 2003/10/21 22:30:33 pelle
37 Renamed NeudistException to NeuClearException and moved it to org.neuclear.commons where it makes more sense.
38 Unhooked the XMLException in the xmlsig library from NeuClearException to make all of its exceptions an independent hierarchy.
39 Obviously had to perform many changes throughout the code to support these changes.
40
41 Revision 1.3 2003/09/26 23:52:47 pelle
42 Changes mainly in receiver and related fun.
43 First real neuclear stuff in the payment package. Added TransferContract and AssetControllerReceiver.
44
45 Revision 1.2 2003/02/11 14:50:24 pelle
46 Trying onemore time. Added the benchmarking code.
47 Now generates DigestValue and optionally adds KeyInfo to Signature.
48
49 Revision 1.1 2003/02/08 20:55:08 pelle
50 Some documentation changes.
51 Major reorganization of code. The code is slowly being cleaned up in such a way that we can
52 get rid of the org.neuclear.utils package and split out the org.neuclear.xml.soap package.
53 Got rid of tons of unnecessary dependencies.
54
55 Revision 1.1 2003/01/18 18:12:32 pelle
56 First Independent commit of the Independent XML-Signature API for NeuDist.
57
58 Revision 1.1 2003/01/16 19:16:09 pelle
59 Major Structural Changes.
60 We've split the test classes out of the normal source tree, to enable Maven's test support to work.
61 WARNING
62 for Development purposes the code does not as of this commit until otherwise notified actually verifysigs.
63 We are reworking the XMLSig library and need to continue work elsewhere for the time being.
64 DO NOT USE THIS FOR REAL APPS
65
66 Revision 1.2 2002/09/21 23:11:16 pelle
67 A bunch of clean ups. Got rid of as many hard coded URL's as I could.
68
69 Revision 1.1.1.1 2002/09/18 10:55:55 pelle
70 First release in new CVS structure.
71 Also first public release.
72 This implemnts simple named objects.
73 - Identity Objects
74 - NSAuth Objects
75
76 Storage systems
77 - In Memory Storage
78 - Clear text file based storage
79 - Encrypted File Storage (with SHA256 digested filenames)
80 - CachedStorage
81 - SoapStorage
82
83 Simple SOAP client/server
84 - Simple Single method call SOAP client, for arbitrary dom4j based requests
85 - Simple Abstract SOAP Servlet for implementing http based SOAP Servers
86
87 Simple XML-Signature Implementation
88 - Based on dom4j
89 - SHA-RSA only
90 - Very simple (likely imperfect) highspeed canonicalizer
91 - Zero support for X509 (We dont like that anyway)
92 - Super Simple
93
94
95 Revision 1.1.1.1 2002/05/29 10:02:22 pelle
96 Lets try one more time. This is the first rev of the next gen of Neudist
97
98
99 Revision 1.1.1.1 2002/03/20 00:46:52 pelleb
100 no message
101
102
103
104 */
105
106 package org.neuclear.commons.crypto;
107
108 import junit.framework.Test;
109 import junit.framework.TestCase;
110 import junit.framework.TestSuite;
111
112 import java.security.KeyPair;
113 import java.security.NoSuchAlgorithmException;
114 import java.security.PrivateKey;
115 import java.util.HashSet;
116
117
118 /***
119 * @author Pelle Braendgaard
120 */
121 public final class CryptoToolsTest extends TestCase {
122 public CryptoToolsTest() {
123 super("CryptoToolsTest");
124 setUp();
125 }
126
127 public CryptoToolsTest(final String name) {
128 super(name);
129 }
130
131 /***
132 */
133 protected final void setUp() {
134 }
135
136 protected final void tearDown() {
137 }
138
139 public static Test suite() {
140
141
142 return new TestSuite(CryptoToolsTest.class);
143 }
144
145 public final void testPadding() {
146 final byte[] src = new byte[20];
147 for (int i = 0; i < src.length; i++)
148 src[i] = (byte) ("a".getBytes()[0] + i);
149 System.out.println("Source array='" + new String(src));
150 final byte[] padded = CryptoTools.pad(src, 14);
151 System.out.println("Dest array='" + new String(padded));
152 assertEquals(padded.length % 14, 0);
153 }
154
155 public final void testSymmetricKeyEncryption() throws CryptoException {
156 final String contentString = "<xml>Hello</xml>";
157 final byte[] password = "Three Brown Geese sledded down the hill".getBytes();
158 final byte[] contents = contentString.getBytes();
159 final byte[] encrypted = CryptoTools.encrypt(password, contents);
160 final String encryptString = new String(encrypted);
161 final byte[] decrypted = CryptoTools.decrypt(password, encrypted);
162 final byte[] depadded = new byte[contents.length];
163 System.arraycopy(decrypted, 0, depadded, 0, depadded.length);
164 final String decryptString = new String(depadded);
165 System.out.println("Original: " + contentString);
166 System.out.println("Encrypted: " + encryptString);
167 System.out.println("Decrypted: " + decryptString);
168
169 assertEquals("Test Encryption/Decryption works", contentString, decryptString);
170 }
171
172 public final void testKeyWrapping() throws NoSuchAlgorithmException, CryptoException {
173 KeyPair kp = CryptoTools.createTinyRSAKeyPair();
174 assertNotNull(kp);
175 assertNotNull(kp.getPrivate());
176 char password[] = "the secrets of the world are mine".toCharArray();
177 byte wrapped[] = CryptoTools.wrapKey(password, kp.getPrivate());
178 assertNotNull(wrapped);
179 PrivateKey priv = CryptoTools.unWrapRSAKey(password, wrapped);
180
181 byte[] data = "the quick brown fox jumped over the lazy dog".getBytes();
182 byte[] sig = CryptoTools.sign(priv, data);
183 assertNotNull(sig);
184 assertTrue(CryptoTools.verify(kp.getPublic(), data, sig));
185 }
186 /*
187
188 public final void testRSAKeyClash() throws NoSuchAlgorithmException {
189 System.out.println("RSA1024");
190 testRSAKeyClash(1); // To bring the JIT up to speed
191 final int iterations = 1000;
192 final long start = System.currentTimeMillis();
193 testRSAKeyClash(iterations);
194 final long dur = System.currentTimeMillis() - start;
195 System.out.println("Total Duration " + dur / 1000 + "s");
196 System.out.println("KPG Duration " + dur / iterations + "ms");
197
198 }
199
200 public final void testRSAKeyClash(final int iterations) throws NoSuchAlgorithmException {
201 HashSet set = new HashSet();
202 for (int i = 0; i < iterations; i++) {
203 if ((i % 10) == 1)
204 System.out.print(".");
205 String key = CryptoTools.encodeBase32(CryptoTools.digest(CryptoTools.createKeyPair("RSA").getPublic().getEncoded()));
206 assertFalse(set.contains(key));
207 set.add(key);
208 }
209 }
210
211 public final void testTinyRSAKeyClash() throws NoSuchAlgorithmException {
212 System.out.println("RSA512");
213 testTinyRSAKeyClash(1); // To bring the JIT up to speed
214 final int iterations = 1000;
215 final long start = System.currentTimeMillis();
216 testTinyRSAKeyClash(iterations);
217 final long dur = System.currentTimeMillis() - start;
218 System.out.println("Total Duration " + dur / 1000 + "s");
219 System.out.println("KPG Duration " + dur / iterations + "ms");
220
221 }
222
223 public final void testTinyRSAKeyClash(final int iterations) throws NoSuchAlgorithmException {
224 HashSet set = new HashSet();
225 for (int i = 0; i < iterations; i++) {
226 if ((i % 10) == 1)
227 System.out.print(".");
228 String key = CryptoTools.encodeBase32(CryptoTools.digest(CryptoTools.createTinyRSAKeyPair().getPublic().getEncoded()));
229 assertFalse(set.contains(key));
230 set.add(key);
231 }
232 }
233
234 public final void testTinyDSAKeyClash() throws NoSuchAlgorithmException {
235 System.out.println("DSA512");
236
237 testTinyDSAKeyClash(1); // To bring the JIT up to speed
238 final int iterations = 1000;
239 final long start = System.currentTimeMillis();
240 testTinyDSAKeyClash(iterations);
241 final long dur = System.currentTimeMillis() - start;
242 System.out.println("Total Duration " + dur / 1000 + "s");
243 System.out.println("KPG Duration " + dur / iterations + "ms");
244
245 }
246
247 public final void testTinyDSAKeyClash(final int iterations) throws NoSuchAlgorithmException {
248 HashSet set = new HashSet();
249 for (int i = 0; i < iterations; i++) {
250 if ((i % 10) == 1)
251 System.out.print(".");
252 String key = CryptoTools.encodeBase32(CryptoTools.digest(CryptoTools.createTinyDSAKeyPair().getPublic().getEncoded()));
253 assertFalse(set.contains(key));
254 set.add(key);
255 }
256 }
257
258 public final void testDSAKeyClash() throws NoSuchAlgorithmException {
259 System.out.println("DSA1024");
260 testDSAKeyClash(1); // To bring the JIT up to speed
261 final int iterations = 1000;
262 final long start = System.currentTimeMillis();
263 testDSAKeyClash(iterations);
264 final long dur = System.currentTimeMillis() - start;
265 System.out.println("Total Duration " + dur / 1000 + "s");
266 System.out.println("KPG Duration " + dur / iterations + "ms");
267
268 }
269
270 public final void testDSAKeyClash(final int iterations) throws NoSuchAlgorithmException {
271 HashSet set = new HashSet();
272 for (int i = 0; i < iterations; i++) {
273 if ((i % 10) == 1)
274 System.out.print(".");
275 String key = CryptoTools.encodeBase32(CryptoTools.digest(CryptoTools.createKeyPair("DSA").getPublic().getEncoded()));
276 assertFalse(set.contains(key));
277 set.add(key);
278 }
279 }
280 */
281
282 }
This page was automatically generated by Maven