KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > lightcrypto > test > DigestTest


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

20
21 package net.sourceforge.lightcrypto.test;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import net.sourceforge.lightcrypto.Digesters;
26
27 import java.io.DataOutputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * A collection of digest tests
33  * <P>
34  * These tests can be run using JUnit (http://www.junit.org)
35  *
36  * @author Gert Van Ham
37  * @author hamgert@users.sourceforge.net
38  * @author http://jcetaglib.sourceforge.net
39  * @version $Id: DigestTest.java,v 1.2 2003/10/05 11:41:29 hamgert Exp $
40  */

41
42 public class DigestTest extends TestCase {
43     private StringBuffer JavaDoc text1;
44     private StringBuffer JavaDoc text2;
45     private StringBuffer JavaDoc text3;
46     private StringBuffer JavaDoc text4;
47     private StringBuffer JavaDoc text5;
48     private StringBuffer JavaDoc text6;
49     private StringBuffer JavaDoc text7;
50     private StringBuffer JavaDoc text8;
51     private StringBuffer JavaDoc text9;
52     private StringBuffer JavaDoc text10;
53
54     /**
55      * setup test
56      *
57      * @throws IOException
58      */

59     protected void setUp() throws IOException JavaDoc {
60         text1 = new StringBuffer JavaDoc("The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms");
61         text2 = new StringBuffer JavaDoc("This software is distributed under a license based on the MIT X Consortium license");
62         text3 = new StringBuffer JavaDoc("found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution");
63         text4 = new StringBuffer JavaDoc("Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können");
64         text5 = new StringBuffer JavaDoc("En av de största nyheterna är att det finns en .NET Enterprise Server-lösning för stora företagsomspännade projekt");
65         text6 = new StringBuffer JavaDoc("Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET");
66         text7 = new StringBuffer JavaDoc("Vergeet even die oude tovenaars met puntige hoeden en rondborstige jonkvrouwen in nood... oké, vergeet in ieder geval even die tovenaars, want Lionheart komt met een ambitieuze rollenspelvariant");
67         text8 = new StringBuffer JavaDoc("An implementation of ECIES (stream mode) as described in IEEE P 1363a.");
68         text9 = new StringBuffer JavaDoc("This makes the entire keystore resistant to tampering and inspection, and forces verification");
69         text10 = new StringBuffer JavaDoc("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
70
71         // create text file
72
FileOutputStream JavaDoc outStr = new FileOutputStream JavaDoc(RunTest.TEMPFOLDER + "readable.txt");
73         DataOutputStream JavaDoc dataStr = new DataOutputStream JavaDoc(outStr);
74
75         dataStr.writeBytes("This is a readable string inside a file");
76
77         dataStr.flush();
78         dataStr.close();
79
80         outStr.close();
81     }
82
83     /**
84      * test digest
85      *
86      * @throws Exception
87      */

88     public void testDigest() throws Exception JavaDoc {
89         Assert.assertEquals(Digesters.digest(text1, null).toString(), "GPMnLEpblugEcs2kmFkg3Q==");
90         Assert.assertEquals(Digesters.digest(text2, null).toString(), "LPcC8hTqwv/qBcDUQdpx4w==");
91         Assert.assertEquals(Digesters.digest(text3, null).toString(), "1pWkqCss4OvVPCv0fcSBgQ==");
92         Assert.assertEquals(Digesters.digest(text4, null).toString(), "6kqKaRJHN1an+j+u2fUvHA==");
93         Assert.assertEquals(Digesters.digest(text5, null).toString(), "KJ+Ve5/s8HDv/xu49lsf3g==");
94         Assert.assertEquals(Digesters.digest(text6, null).toString(), "a7syvGijznXELm/sOctixw==");
95         Assert.assertEquals(Digesters.digest(text7, null).toString(), "Pw3X59NMqZEYaOyKDPXn1g==");
96         Assert.assertEquals(Digesters.digest(text8, null).toString(), "uA+eNem45Shm+4SWImwFDw==");
97         Assert.assertEquals(Digesters.digest(text9, null).toString(), "jMU70cRMB3LnWkWrWhczBg==");
98         Assert.assertEquals(Digesters.digest(text10, null).toString(), "BytgzY4DVd4pFgyQLK1rMw==");
99
100         // try some other algorithms
101
Assert.assertEquals(Digesters.digest(text8, "SHA1").toString(), "U81M41/RyigieCM/7xnIfO9cDyY=");
102     }
103
104     /**
105      * test file digest
106      *
107      * @throws Exception
108      */

109     public void testFileDigest() throws Exception JavaDoc {
110         Assert.assertEquals(Digesters.digestFromFile(RunTest.TEMPFOLDER + "readable.txt", null).toString(), "oHHF8dn23j348n/jrII7nA==");
111     }
112 }
113
Popular Tags