1 package org.neuclear.commons.crypto;
2
3 import java.security.KeyPair;
4 import java.security.KeyPairGenerator;
5 import java.security.NoSuchAlgorithmException;
6 import java.util.Date;
7
8 /*
9 NeuClear Distributed Transaction Clearing Platform
10 (C) 2003 Pelle Braendgaard
11
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2.1 of the License, or (at your option) any later version.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26 $Id: SigningBenchmark.java,v 1.3 2003/11/21 04:43:42 pelle Exp $
27 $Log: SigningBenchmark.java,v $
28 Revision 1.3 2003/11/21 04:43:42 pelle
29 EncryptedFileStore now works. It uses the PBECipher with DES3 afair.
30 Otherwise You will Finaliate.
31 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.
32 This should hopefully make everything more stable (and secure).
33
34 Revision 1.2 2003/11/12 23:47:50 pelle
35 Much work done in creating good test environment.
36 PaymentReceiverTest works, but needs a abit more work in its environment to succeed testing.
37
38 Revision 1.1 2003/11/12 18:54:42 pelle
39 Updated SimpleSignerStoreTest to use a StoredPassPhraseAgent eliminating the popup during testing.
40 Created SigningBenchmark for running comparative performance benchmarks on various key algorithms.
41
42 */
43
44 /***
45 * Tests performances of various cryptographic such as key generation
46 * signing and verification.
47 */
48 public final class SigningBenchmark {
49 public SigningBenchmark(final String algorithm, final int size) throws NoSuchAlgorithmException {
50 kpg = KeyPairGenerator.getInstance(algorithm);
51 kpg.initialize(size);
52 results = new long[3];
53 this.algorithm = algorithm;
54 this.size = size;
55 testdata = TESTSTRING.getBytes();
56 }
57
58
59 public final void generateKeys() {
60 kp = kpg.generateKeyPair();
61 }
62
63 public final void sign() throws CryptoException {
64 testsig = CryptoTools.sign(kp, testdata);
65 }
66
67 public final void verify() throws CryptoException {
68 CryptoTools.verify(kp.getPublic(), testdata, testsig);
69 }
70
71 public final void benchmark() throws CryptoException {
72
73 Date start = new Date();
74 for (int i = 0; i < ITERATIONS; i++)
75 generateKeys();
76 Date end = new Date();
77 results[0] = end.getTime() - start.getTime();
78
79 start = new Date();
80 for (int i = 0; i < ITERATIONS; i++)
81 sign();
82 end = new Date();
83 results[1] = end.getTime() - start.getTime();
84
85 start = new Date();
86 for (int i = 0; i < ITERATIONS; i++)
87 verify();
88 end = new Date();
89 results[2] = (end.getTime() - start.getTime()) / ITERATIONS;
90 printResults();
91 }
92
93 public final void printResults() {
94
95 System.out.println(algorithm + "\t" + size + "\t" + results[0] + "\t" + results[1] + "\t" + results[2]);
96 }
97
98 public static void main(final String[] args) {
99 System.out.println("Performance Analysis of key sizes and algorithms");
100
101 final String[] algorithms = new String[]{"RSA", "DSA"};
102 final int[] keysizes = new int[]{512, 1024};
103 try {
104 for (int a = 0; a < algorithms.length; a++)
105 for (int k = 0; k < keysizes.length; k++)
106 new SigningBenchmark(algorithms[a], keysizes[k]).benchmark();
107 } catch (CryptoException e) {
108 e.printStackTrace();
109 } catch (NoSuchAlgorithmException e) {
110 e.printStackTrace();
111 }
112
113 }
114
115 private static final int ITERATIONS = 100;
116 private final KeyPairGenerator kpg;
117
118 private KeyPair kp;
119 private final String algorithm;
120 private final int size;
121
122 private final long[] results;
123
124 private static final String TESTSTRING = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
125 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
126 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
127 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
128 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
129 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
130 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
131 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
132 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
133 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
134 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
135 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
136 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
137 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
138 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
139 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
140 private byte[] testsig;
141 private final byte[] testdata;
142
143 }
This page was automatically generated by Maven