KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: net.sourceforge.lightcrypto.HMacs
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.digests.SHA1Digest;
24 import org.bouncycastle.crypto.macs.HMac;
25 import org.bouncycastle.util.encoders.Base64;
26
27 import java.io.*;
28
29 /**
30  * Create HMAC (Hash Message Authentication Code) from text and files
31  *
32  * @author Gert Van Ham
33  * @author hamgert@users.sourceforge.net
34  * @author http://jcetaglib.sourceforge.net
35  * @version $Id: HMacs.java,v 1.1 2003/10/05 11:41:29 hamgert Exp $
36  */

37
38 public class HMacs {
39     private static int BUFFERSIZE_TEXT = 64;
40     private static int BUFFERSIZE_FILE = 8192;
41
42     /**
43      * Creates an HMAC from a stringbuffer
44      *
45      * @param text creates HMAC from this text
46      * @return HMAC in BASE64 format
47      * @throws CryptoException for HMAC errors
48      */

49     public static StringBuffer JavaDoc mac(
50             StringBuffer JavaDoc text
51             ) throws CryptoException {
52         return mac(new ByteArrayInputStream(text.toString().getBytes()), BUFFERSIZE_TEXT);
53     }
54
55     /**
56      * Creates an HMAC from an inputstream
57      *
58      * @param is any inputstream
59      * @param buffersize the buffersize in number of bytes
60      * @return HMAC in BASE64 format
61      * @throws CryptoException for HMAC errors
62      */

63     public static StringBuffer JavaDoc mac(
64             InputStream is
65             , int buffersize
66             ) throws CryptoException {
67
68         try {
69             HMac hmac = new HMac(new SHA1Digest());
70
71             byte[] result = new byte[hmac.getMacSize()];
72             byte[] buffer = new byte[buffersize];
73             int length = 0;
74
75             // read bytes into buffer and feed these bytes into Hmac object
76
while ((length = is.read(buffer)) != -1) {
77                 hmac.update(buffer, 0, length);
78             }
79
80             hmac.doFinal(result, 0);
81
82             return new StringBuffer JavaDoc(new String JavaDoc(Base64.encode(result)));
83         } catch (Exception JavaDoc ex) {
84             ex.printStackTrace();
85             throw new CryptoException(ex.getMessage());
86         }
87     }
88
89     /**
90      * Creates an HMAC from a file
91      *
92      * @param file filename
93      * @return HMAC in BASE64 format
94      * @throws CryptoException for HMAC errors
95      * @throws FileNotFoundException when the file was not found
96      * @throws IOException when the file could not be opened (or closed)
97      */

98     public static StringBuffer JavaDoc macFromFile(
99             String JavaDoc file
100             ) throws CryptoException, FileNotFoundException, IOException {
101
102         FileInputStream fis = new FileInputStream(file);
103         StringBuffer JavaDoc res = mac(fis, BUFFERSIZE_FILE);
104         fis.close();
105         return res;
106     }
107 }
108
Popular Tags