KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: Digest.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.Digesters;
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 message digests (hashing) of strings and files
33  *
34  * @jsp.tag
35  * name="digest"
36  * display-name="Digest"
37  * body-content="JSP"
38  * example="
39  * <%-- Create a message digest from the body of the tag using the MD5 algorithm --%>

40  * <jce:digest algorithm=\"MD5\">

41  * This was a normal text

42  * </jce:digest>"
43  *
44  * description="JSP tag used for creating message digests (hashing) of strings and files"
45  *
46  * @author Gert Van Ham
47  * @author hamgert@users.sourceforge.net
48  * @author http://jcetaglib.sourceforge.net
49  * @version $Id: Digest.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
50  */

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

62     private String JavaDoc algorithm = "Tiger"; // tag attribute
63
private String JavaDoc file; // tag attribute
64

65     private StringBuffer JavaDoc input; // what we'll store in scope:var
66
private StringBuffer JavaDoc output; // return text
67

68     public static int getScope(String JavaDoc scope) {
69         int ret = PageContext.PAGE_SCOPE; // default
70

71         if (REQUEST.equalsIgnoreCase(scope))
72             ret = PageContext.REQUEST_SCOPE;
73         else if (SESSION.equalsIgnoreCase(scope))
74             ret = PageContext.SESSION_SCOPE;
75         else if (APPLICATION.equalsIgnoreCase(scope))
76             ret = PageContext.APPLICATION_SCOPE;
77         else if (PAGE.equalsIgnoreCase(scope))
78             ret = PageContext.PAGE_SCOPE;
79
80         return ret;
81     } //getScope()
82

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

129     public void release() {
130         // Cleanup all sensitive information
131
Clean.blank(value);
132         Clean.blank(input);
133         Clean.blank(output);
134
135         super.release();
136     } //release()
137

138     /**
139      * @jsp.attribute
140      * description="Optional attribute, the string to hash. The body of the tag will be taken if omitted"
141      * type="java.lang.StringBuffer"
142      * required="false"
143      * rtexprvalue="true"
144      */

145     public void setValue(StringBuffer JavaDoc value) {
146         this.value = value;
147     }
148
149     public StringBuffer JavaDoc getValue() {
150         return value;
151     }
152
153     /**
154      * @jsp.attribute
155      * description="Optional attribute, variable to store the message digest. The hashed string will be printed if omitted"
156      * type="java.lang.String"
157      * required="false"
158      * rtexprvalue="false"
159      */

160     public void setVar(String JavaDoc var) {
161         this.var = var;
162     }
163
164     public String JavaDoc getVar() {
165         return var;
166     }
167
168     /**
169      * @jsp.attribute
170      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
171      * type="java.lang.String"
172      * required="false"
173      * rtexprvalue="false"
174      */

175     public void setScope(String JavaDoc scope) {
176         this.scope = getScope(scope);
177     }
178
179     /**
180      * @jsp.attribute
181      * description="The hash algorithm. Default is 'Tiger'"
182      * type="java.lang.String"
183      * required="false"
184      * rtexprvalue="true"
185      */

186     public void setAlgorithm(String JavaDoc algorithm) {
187         this.algorithm = algorithm;
188     }
189
190     /**
191      * @jsp.attribute
192      * description="Create a message digest from a file instead of a string"
193      * type="java.lang.String"
194      * required="false"
195      * rtexprvalue="true"
196      */

197     public void setFile(String JavaDoc file) {
198         this.file = file;
199     }
200 }
Popular Tags