KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > lib > Digesters


1 /*
2   Name: Digesters.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.lib;
23
24 import net.sourceforge.jcetaglib.exceptions.CryptoException;
25 import org.bouncycastle.jce.provider.BouncyCastleProvider;
26 import org.bouncycastle.util.encoders.Base64;
27
28 import java.io.*;
29 import java.security.Key JavaDoc;
30 import java.security.MessageDigest JavaDoc;
31 import java.security.Security JavaDoc;
32
33 /**
34  * Digest/hash routines for use with BouncyCastle JCE provider
35  *
36  * @author Gert Van Ham
37  * @author hamgert@users.sourceforge.net
38  * @author http://jcetaglib.sourceforge.net
39  * @version $Id: Digesters.java,v 1.3 2004/04/15 07:28:25 hamgert Exp $
40  */

41 public class Digesters {
42     // buffersizes in bytes
43
private static int BUFFERSIZE_TEXT = 64;
44     private static int BUFFERSIZE_FILE = 8192;
45
46     /**
47      * Returns a message digest (hash) from a text
48      *
49      * @param text text to create message digest from
50      * @param algorithm hash algorithm (e.g. "Tiger")
51      * @return message digest in BASE64 format
52      * @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
53      */

54     public static StringBuffer JavaDoc hash(StringBuffer JavaDoc text
55                                     , String JavaDoc algorithm)
56             throws CryptoException {
57
58         ByteArrayOutputStream bao = null;
59         DataOutputStream dao = null;
60
61         try {
62             bao = new ByteArrayOutputStream();
63             dao = new DataOutputStream(bao);
64
65             // hash text
66
hash(new ByteArrayInputStream(text.toString().getBytes()), dao, algorithm, BUFFERSIZE_TEXT);
67
68             return new StringBuffer JavaDoc(new String JavaDoc(Base64.encode(bao.toByteArray())));
69         } catch (IOException ioe) {
70             ioe.printStackTrace();
71             throw new CryptoException(ioe.getMessage());
72         } finally {
73             if (dao != null) {
74                 // close outputstream
75
try {
76                     dao.close();
77                 } catch (IOException e) {
78                     ;
79                 }
80             }
81         }
82     }
83
84     /**
85      * Returns a message digest (hash) from a file
86      *
87      * @param file the filename/location
88      * @param algorithm hash algorithm (e.g. "Tiger")
89      * @return file digest in BASE64 format
90      * @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
91      */

92     public static StringBuffer JavaDoc hashFile(String JavaDoc file
93                                         , String JavaDoc algorithm)
94             throws CryptoException {
95
96         FileInputStream fis = null;
97         ByteArrayOutputStream bao = null;
98         DataOutputStream dao = null;
99
100         try {
101             fis = new FileInputStream(file);
102             bao = new ByteArrayOutputStream();
103             dao = new DataOutputStream(bao);
104
105             // hash file
106
hash(fis, dao, algorithm, BUFFERSIZE_FILE);
107
108             return new StringBuffer JavaDoc(new String JavaDoc(Base64.encode(bao.toByteArray())));
109
110         } catch (IOException ioe) {
111             ioe.printStackTrace();
112             throw new CryptoException(ioe.getMessage());
113         } finally {
114             if (dao != null) {
115                 // close outputstream
116
try {
117                     dao.close();
118                 } catch (IOException e) {
119                     ;
120                 }
121             }
122             if (fis != null) {
123                 // close outputstream
124
try {
125                     fis.close();
126                 } catch (IOException e) {
127                     ;
128                 }
129             }
130         }
131     }
132
133     /**
134      * Returns a message digest (hash) from an inputstream (one-way encryption)
135      *
136      * @param is any inputstream to hash
137      * @param daos digest outputstream
138      * @param algorithm hash algorithm (e.g. "Tiger")
139      * @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
140      * @throws IOException I/O errors
141      **/

142     public static void hash(InputStream is
143                             , DataOutputStream daos
144                             , String JavaDoc algorithm
145                             , int bufferlength)
146             throws CryptoException, IOException {
147         try {
148             // Add Bouncy Castle provider
149
Security.addProvider(new BouncyCastleProvider());
150
151             MessageDigest JavaDoc digest = MessageDigest.getInstance(algorithm, "BC");
152
153             byte[] buffer = new byte[bufferlength];
154             int length = 0;
155
156             // Read bytes into buffer and feed these bytes into the message
157
// digest object.
158
while ((length = is.read(buffer)) != -1) {
159                 digest.update(buffer, 0, length);
160             }
161             // Run the algorithm, so it produces the message digest.
162
byte[] result = digest.digest();
163             daos.write(result);
164         } catch (IOException ioe) {
165             ioe.printStackTrace();
166             throw new IOException(ioe.getMessage());
167         } catch (Exception JavaDoc ex) {
168             ex.printStackTrace();
169             throw new CryptoException(ex.getMessage());
170         }
171     }
172
173     /**
174      * Creates a form digest string (= return digest from a text appended to a symmetric key)
175      *
176      * @param text StringBuffer form string
177      * @param digest String the digest/hash algorithm (e.g. MD5)
178      * @param keyfile String the keystore file(name)
179      * @param passphrase StringBuffer the passphrase for the keystore
180      * @param algorithm String encryption algorithm (e.g. "Rijndael")
181      * @return form digest in BASE64 format
182      * @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
183      * @throws IOException I/O errors
184      **/

185     public static StringBuffer JavaDoc formDigest(StringBuffer JavaDoc text
186                                           , String JavaDoc digest
187                                           , String JavaDoc keyfile
188                                           , StringBuffer JavaDoc passphrase
189                                           , String JavaDoc algorithm)
190             throws CryptoException, IOException {
191
192         Key JavaDoc key = null;
193         ByteArrayOutputStream outStr = null;
194         DataOutputStream dataStr = null;
195
196         try {
197             Security.addProvider(new BouncyCastleProvider());
198
199             // read secret key
200
key = Keystore.loadKey(algorithm, keyfile, passphrase);
201
202             // concatenate secret key & form text
203
outStr = new ByteArrayOutputStream();
204             dataStr = new DataOutputStream(outStr);
205
206             dataStr.write(key.getEncoded());
207             dataStr.writeBytes(text.toString());
208
209             // create hash from the concatenated string
210
return hash(new StringBuffer JavaDoc(outStr.toString()), digest);
211         } catch (IOException ioe) {
212             ioe.printStackTrace();
213             throw new IOException(ioe.getMessage());
214         } catch (Exception JavaDoc ex) {
215             ex.printStackTrace();
216             throw new CryptoException(ex.getMessage());
217         } finally {
218             if (dataStr != null) {
219                 // close outputstream
220
try {
221                     dataStr.close();
222                 } catch (IOException e) {
223                     ;
224                 }
225             }
226         }
227     }
228 }
229
Popular Tags