KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > lightcrypto > Digesters


1 /*
2   Name: net.sourceforge.lightcrypto.Digesters
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;
22
23 import org.bouncycastle.crypto.Digest;
24 import org.bouncycastle.crypto.digests.MD5Digest;
25 import org.bouncycastle.crypto.digests.SHA1Digest;
26 import org.bouncycastle.crypto.digests.SHA256Digest;
27 import org.bouncycastle.util.encoders.Base64;
28
29 import java.io.*;
30
31 /**
32  * digest and hash routines for use with the BouncyCastle lightweight API
33  *
34  * @author Gert Van Ham
35  * @author hamgert@users.sourceforge.net
36  * @author http://jcetaglib.sourceforge.net
37  * @version $Id: Digesters.java,v 1.2 2003/10/05 11:41:29 hamgert Exp $
38  */

39
40 public class Digesters {
41     private static int BUFFERSIZE_TEXT = 64;
42     private static int BUFFERSIZE_FILE = 8192;
43
44     /**
45      * Creates a digest from a stringbuffer
46      *
47      * @param text creates the digest from this text
48      * @param algorithm the digest algorithm. Can be 'SHA1' or 'MD5' (default)
49      * @return the generated digest string in BASE64
50      * @exception CryptoException for all errors
51      **/

52     public static StringBuffer JavaDoc digest(
53             StringBuffer JavaDoc text
54             , String JavaDoc algorithm) throws CryptoException {
55         return digest(new ByteArrayInputStream(text.toString().getBytes()), algorithm, BUFFERSIZE_TEXT);
56     }
57
58     /**
59      * Creates a digest from an inputstream
60      *
61      * @param is inputstream
62      * @param algorithm the digest algorithm. Can be 'SHA1' or 'MD5' (default)
63      * @return the generated digest string in BASE64
64      * @exception CryptoException for all errors
65      **/

66     public static StringBuffer JavaDoc digest(
67             InputStream is
68             , String JavaDoc algorithm
69             , int buffersize
70             ) throws CryptoException {
71         Digest digest;
72
73         try {
74             if (algorithm != null) {
75                 if (algorithm.equalsIgnoreCase("SHA1")) {
76                     digest = new SHA1Digest();
77                 } else if (algorithm.equalsIgnoreCase("SHA256")) {
78                     digest = new SHA256Digest();
79                 } else {
80                     digest = new MD5Digest();
81                 }
82             } else {
83                 digest = new MD5Digest();
84             }
85
86             byte[] result = new byte[digest.getDigestSize()];
87             byte[] buffer = new byte[buffersize];
88             int length = 0;
89
90             // read bytes into buffer and feed these bytes into the message digest object
91
while ((length = is.read(buffer)) != -1) {
92                 digest.update(buffer, 0, length);
93             }
94
95             digest.doFinal(result, 0);
96
97             return new StringBuffer JavaDoc(new String JavaDoc(Base64.encode(result)));
98         } catch (Exception JavaDoc ex) {
99             ex.printStackTrace();
100             throw new CryptoException(ex.getMessage());
101         }
102     }
103
104      /**
105      * Creates a digest from a file
106      *
107      * @param file creates the digest from this file
108      * @param algorithm the digest algorithm. Can be 'SHA1' or 'MD5' (default)
109      * @return the generated digest string in BASE64
110      * @exception CryptoException for digest errors
111      * @throws FileNotFoundException when the file was not found
112      * @throws IOException when the file could not be opened (or closed)
113      **/

114      public static StringBuffer JavaDoc digestFromFile(
115             String JavaDoc file
116              , String JavaDoc algorithm
117             ) throws CryptoException, FileNotFoundException, IOException {
118
119         FileInputStream fis = new FileInputStream(file);
120         StringBuffer JavaDoc res = digest(fis, algorithm, BUFFERSIZE_FILE);
121         fis.close();
122         return res;
123     }
124 }
125
Popular Tags