KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > test > SignatureTest


1 /*
2   Name: SignatureTest.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18
19   (C) Copyright 2003 Gert Van Ham
20 */

21
22 package net.sourceforge.jcetaglib.test;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 import net.sourceforge.jcetaglib.lib.Signatures;
27 import net.sourceforge.jcetaglib.lib.X509Cert;
28
29 import java.io.DataOutputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.security.PrivateKey JavaDoc;
33 import java.security.cert.X509Certificate JavaDoc;
34
35 /**
36  * A collection of SIG (siganture) tests
37  * <P>
38  * These tests can be run using JUnit (http://www.junit.org)
39  *
40  * @author Gert Van Ham
41  * @author hamgert@users.sourceforge.net
42  * @author http://jcetaglib.sourceforge.net
43  * @version $Id: SignatureTest.java,v 1.1 2003/12/28 19:06:57 hamgert Exp $
44  */

45 public class SignatureTest extends TestCase {
46     private StringBuffer JavaDoc sig = null;
47
48     /**
49      * setup test
50      *
51      * @throws java.io.IOException
52      */

53     protected void setUp() throws IOException JavaDoc {
54         // create text file
55
FileOutputStream JavaDoc outStr = new FileOutputStream JavaDoc(RunTest.TEMPFOLDER + "readable.txt");
56         DataOutputStream JavaDoc dataStr = new DataOutputStream JavaDoc(outStr);
57
58         dataStr.writeBytes("This is a readable string inside a file");
59
60         dataStr.flush();
61         dataStr.close();
62
63         outStr.close();
64     }
65
66     /**
67      * test Signature
68      *
69      * @throws Exception
70      */

71     public void testSignature() throws Exception JavaDoc {
72         // load Alice's certificate
73
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
74
75         // load Alice's private key to decrypt
76
PrivateKey JavaDoc aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
77
78         for (int i = 0; i < RunTest.sig.length; i++) {
79             for (int j = 0; j < RunTest.text.length; j++) {
80                 sig = Signatures.generateSIG(RunTest.text[j], aliceprivate, RunTest.sig[i]);
81
82                 Assert.assertTrue(Signatures.verifySIG(RunTest.text[j], sig, alicecert.getPublicKey(), RunTest.sig[i]));
83             }
84         }
85     }
86
87     /**
88      * test Signature from file
89      *
90      * @throws Exception
91      */

92     public void testFileSignature() throws Exception JavaDoc {
93         // load Alice's certificate
94
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
95
96         // load Alice's private key to decrypt
97
PrivateKey JavaDoc aliceprivate = X509Cert.getPrivateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
98
99         for (int i = 0; i < RunTest.sig.length; i++) {
100             sig = Signatures.generateFileSIG(RunTest.TEMPFOLDER + "readable.txt", aliceprivate, RunTest.sig[i]);
101
102             Assert.assertTrue(Signatures.verifyFileSIG(RunTest.TEMPFOLDER + "readable.txt", sig, alicecert.getPublicKey(), RunTest.sig[i]));
103         }
104     }
105 }
106
Popular Tags