KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: Macs.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 javax.crypto.Mac;
29 import java.io.*;
30 import java.security.Key JavaDoc;
31 import java.security.Security JavaDoc;
32
33 /**
34  * Create MACs (Message Authentication Code) with the BouncyCastle JCE provider
35  *
36  * @author Gert Van Ham
37  * @author hamgert@users.sourceforge.net
38  * @author http://jcetaglib.sourceforge.net
39  * @version $Id: Macs.java,v 1.3 2004/04/15 07:28:25 hamgert Exp $
40  */

41 public class Macs {
42     // buffersizes in bytes
43
private static int BUFFERSIZE_TEXT = 64;
44     private static int BUFFERSIZE_FILE = 8192;
45
46     /**
47      * Returns a MAC (Message Authentication Code) from a text
48      *
49      * @param text text to create MAC from
50      * @param keyfile keyfile(name)
51      * @param passphrase the passphrase for the keystore
52      * @param algorithm encryption algorithm (e.g. "Rijndael")
53      * @param macname MAC algorithm (e.g. IDEAMac)
54      * @return the MAC in BASE64 format
55      * @throws CryptoException for all encryption errors
56      */

57     public static StringBuffer JavaDoc generateMAC(StringBuffer JavaDoc text
58                                            , String JavaDoc keyfile
59                                            , StringBuffer JavaDoc passphrase
60                                            , String JavaDoc algorithm
61                                            , String JavaDoc macname)
62             throws CryptoException {
63
64         ByteArrayOutputStream bao = null;
65         DataOutputStream dao = null;
66
67         try {
68             bao = new ByteArrayOutputStream();
69             dao = new DataOutputStream(bao);
70
71             // Create MAC
72
generateMAC(new ByteArrayInputStream(text.toString().getBytes()), dao, keyfile, passphrase, algorithm, macname, BUFFERSIZE_TEXT);
73
74             return new StringBuffer JavaDoc(new String JavaDoc(Base64.encode(bao.toByteArray())));
75         } catch (IOException ioe) {
76             ioe.printStackTrace();
77             throw new CryptoException(ioe.getMessage());
78         } finally {
79             if (dao != null) {
80                 // close outputstream
81
try {
82                     dao.close();
83                 } catch (IOException e) {
84                     ;
85                 }
86             }
87         }
88     }
89
90     /**
91      * Returns a MAC (Message Authentication Code) from a file
92      *
93      * @param file file to create MAC from
94      * @param keyfile keyfile(name)
95      * @param passphrase the passphrase for the keystore
96      * @param algorithm encryption algorithm (e.g. "Rijndael")
97      * @param macname MAC algorithm (e.g. IDEAMac)
98      * @return the MAC in BASE64 format
99      * @throws CryptoException for encryption errors
100      */

101     public static StringBuffer JavaDoc generateFileMAC(String JavaDoc file
102                                                , String JavaDoc keyfile
103                                                , StringBuffer JavaDoc passphrase
104                                                , String JavaDoc algorithm
105                                                , String JavaDoc macname)
106             throws CryptoException {
107
108         FileInputStream fis = null;
109         ByteArrayOutputStream bao = null;
110         DataOutputStream dao = null;
111
112         try {
113             fis = new FileInputStream(file);
114             bao = new ByteArrayOutputStream();
115             dao = new DataOutputStream(bao);
116
117             // generate mac
118
generateMAC(fis, dao, keyfile, passphrase, algorithm, macname, BUFFERSIZE_FILE);
119
120             return new StringBuffer JavaDoc(new String JavaDoc(Base64.encode(bao.toByteArray())));
121
122         } catch (IOException ioe) {
123             ioe.printStackTrace();
124             throw new CryptoException(ioe.getMessage());
125         } finally {
126             if (dao != null) {
127                 // close outputstream
128
try {
129                     dao.close();
130                 } catch (IOException e) {
131                     ;
132                 }
133             }
134             if (fis != null) {
135                 // close outputstream
136
try {
137                     fis.close();
138                 } catch (IOException e) {
139                     ;
140                 }
141             }
142         }
143     }
144
145     /**
146      * Returns a MAC (Message Authentication Code) from any inputstream
147      *
148      * @param is any inputstream to generate HMAC from
149      * @param daos returns MAC code string outputstream
150      * @param keyfile keyfile(name)
151      * @param passphrase the passphrase for the keystore
152      * @param algorithm encryption algorithm (e.g. "Rijndael")
153      * @param macname MAC algorithm (e.g. IDEAMac)
154      * @throws IOException I/O errors
155      * @throws CryptoException for all encryption errors
156      **/

157     public static void generateMAC(InputStream is
158                                    , DataOutputStream daos
159                                    , String JavaDoc keyfile
160                                    , StringBuffer JavaDoc passphrase
161                                    , String JavaDoc algorithm
162                                    , String JavaDoc macname
163                                    , int bufferlength)
164             throws CryptoException, IOException {
165
166         Key JavaDoc secretKey = null;
167         Mac mac = null;
168
169         try {
170             // Add Bouncy Castle provider
171
Security.addProvider(new BouncyCastleProvider());
172
173             // read secret key
174
secretKey = Keystore.loadKey(algorithm, keyfile, passphrase);
175
176             mac = Mac.getInstance(macname, "BC");
177             mac.init(secretKey);
178
179             byte[] buffer = new byte[bufferlength];
180             int length = 0;
181
182             // Read bytes into buffer
183
while ((length = is.read(buffer)) != -1) {
184                 mac.update(buffer, 0, length);
185             }
186
187             byte[] result = mac.doFinal();
188             daos.write(result);
189         } catch (IOException ioe) {
190             ioe.printStackTrace();
191             throw new IOException(ioe.getMessage());
192         } catch (Exception JavaDoc ex) {
193             ex.printStackTrace();
194             throw new CryptoException(ex.getMessage());
195         }
196     }
197 }
198
Popular Tags