KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > taglib > crypto > HMAC


1 /*
2   Name: HMAC.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
20 package net.sourceforge.jcetaglib.taglib.crypto;
21
22 import net.sourceforge.jcetaglib.lib.Clean;
23 import net.sourceforge.jcetaglib.lib.Macs;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.JspWriter JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * JSP tag used for creating HMAC (message authentication code) of strings and files
33  *
34  * @jsp.tag
35  * name="hmac"
36  * display-name="HMAC"
37  * body-content="JSP"
38  * example="
39  * <%-- Generates and print the MAC from a file --%>

40  * <jce:hmac

41  * 	keyfile=\"c:/jspkey.key\"

42  * 	passphrase=\"<%= new StringBuffer(\"password\") %>\"

43  * 	file=\"c:/zipfile.zip\"/>"
44  *
45  * description="JSP tag used for creating HMAC (message authentication code) of strings and files"
46  *
47  * @author Gert Van Ham
48  * @author hamgert@users.sourceforge.net
49  * @author http://jcetaglib.sourceforge.net
50  * @version 0.6 (beta) 30-oct-2002
51  */

52 public class HMAC extends BodyTagSupport JavaDoc {
53
54     private static final String JavaDoc PAGE = "page";
55     private static final String JavaDoc REQUEST = "request";
56     private static final String JavaDoc SESSION = "session";
57     private static final String JavaDoc APPLICATION = "application";
58
59     private StringBuffer JavaDoc value; // tag attribute
60
private String JavaDoc var; // tag attribute
61
private int scope = PageContext.PAGE_SCOPE; // tag attribute
62

63     private StringBuffer JavaDoc passphrase; // tag attribute
64

65     private String JavaDoc hmacname = "HMac-SHA512"; // tag attribute
66
private String JavaDoc algorithm = "AES";
67     private String JavaDoc file; // tag attribute
68
private String JavaDoc keyfile; // tag attribute
69

70     private StringBuffer JavaDoc input; // what we'll store in scope:var
71
private StringBuffer JavaDoc output; // return text
72

73     public static int getScope(String JavaDoc scope) {
74         int ret = PageContext.PAGE_SCOPE; // default
75

76         if (REQUEST.equalsIgnoreCase(scope))
77             ret = PageContext.REQUEST_SCOPE;
78         else if (SESSION.equalsIgnoreCase(scope))
79             ret = PageContext.SESSION_SCOPE;
80         else if (APPLICATION.equalsIgnoreCase(scope))
81             ret = PageContext.APPLICATION_SCOPE;
82         else if (PAGE.equalsIgnoreCase(scope))
83             ret = PageContext.PAGE_SCOPE;
84
85         return ret;
86     } //getScope()
87

88     public int doEndTag() throws JspException JavaDoc {
89         // determine the value by...
90
if (value != null) {
91             // ... reading our attribute
92
input = value;
93         } else {
94             // ... retrieving and trimming our body
95
if (bodyContent == null || bodyContent.getString() == null) {
96                 input = new StringBuffer JavaDoc("");
97             } else {
98                 input = new StringBuffer JavaDoc(bodyContent.getString().trim());
99             }
100         }
101
102         try {
103             if (file != null) {
104                 output = Macs.generateFileMAC(file, keyfile, passphrase, algorithm, hmacname);
105             } else {
106                 output = Macs.generateMAC(input, keyfile, passphrase, algorithm, hmacname);
107             }
108         } catch (Exception JavaDoc e) {
109             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
110         }
111
112         // decide what to do with the result
113
if (var != null) {
114             if (output != null) {
115                 pageContext.setAttribute(var, output, scope);
116             }
117         } else {
118             if (bodyContent != null) {
119                 bodyContent.clearBody();
120             }
121
122             try {
123                 JspWriter JavaDoc w = pageContext.getOut();
124                 w.print(output);
125             } catch (IOException JavaDoc ex) {
126                 throw new JspException JavaDoc(ex.getMessage(), ex);
127             }
128         }
129
130         // Cleanup all sensitive information
131
input = null;
132
133         return EVAL_PAGE;
134     } // doEndTag()
135

136     public void release() {
137         // Cleanup all sensitive information
138
Clean.blank(value);
139         Clean.blank(passphrase);
140         Clean.blank(input);
141         Clean.blank(output);
142
143         super.release();
144     } //release()
145

146     /**
147      * @jsp.attribute
148      * description="Optional attribute, the string to generate the MAC from. The body of the tag will be taken if omitted. "
149      * type="java.lang.StringBuffer"
150      * required="false"
151      * rtexprvalue="true"
152      */

153     public void setValue(StringBuffer JavaDoc value) {
154         this.value = value;
155     }
156
157     public StringBuffer JavaDoc getValue() {
158         return value;
159     }
160
161     /**
162      * @jsp.attribute
163      * description="Optional attribute, variable to store the MAC. The MAC will be printed if omitted."
164      * required="false"
165      * rtexprvalue="false"
166      */

167     public void setVar(String JavaDoc var) {
168         this.var = var;
169     }
170
171     public String JavaDoc getVar() {
172         return var;
173     }
174
175     /**
176      * @jsp.attribute
177      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'."
178      * required="false"
179      * rtexprvalue="false"
180      */

181     public void setScope(String JavaDoc scope) {
182         this.scope = getScope(scope);
183     }
184
185     /**
186      * @jsp.attribute
187      * description="The MAC algorithm. See the next paragraph under 'MAC' for supported algorithms. Default is 'HMac-SHA512'."
188      * required="false"
189      * rtexprvalue="true"
190      */

191     public void setHmacname(String JavaDoc hmacname) {
192         this.hmacname = hmacname;
193     }
194
195     /**
196      * @jsp.attribute
197      * description="Create a MAC from a file instead of a string"
198      * required="false"
199      * rtexprvalue="true"
200      */

201     public void setFile(String JavaDoc file) {
202         this.file = file;
203     }
204
205     /**
206      * @jsp.attribute
207      * description="The symmetric key file(name)"
208      * required="true"
209      * rtexprvalue="true"
210      */

211     public void setKeyfile(String JavaDoc keyfile) {
212         this.keyfile = keyfile;
213     }
214
215     /**
216      * @jsp.attribute
217      * description="Keystore passphrase"
218      * type="java.lang.StringBuffer"
219      * required="true"
220      * rtexprvalue="true"
221      */

222     public void setPassphrase(StringBuffer JavaDoc passphrase) {
223         this.passphrase = passphrase;
224     }
225
226     /**
227      * @jsp.attribute
228      * description="The key algorithm. Default is 'AES'"
229      * required="false"
230      * rtexprvalue="true"
231      */

232     public void setAlgorithm(String JavaDoc algorithm) {
233         this.algorithm = algorithm;
234     }
235 }
Popular Tags