1 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 ; 26 import javax.servlet.jsp.JspWriter ; 27 import javax.servlet.jsp.PageContext ; 28 import javax.servlet.jsp.tagext.BodyTagSupport ; 29 import java.io.IOException ; 30 31 52 public class HMAC extends BodyTagSupport { 53 54 private static final String PAGE = "page"; 55 private static final String REQUEST = "request"; 56 private static final String SESSION = "session"; 57 private static final String APPLICATION = "application"; 58 59 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 63 private StringBuffer passphrase; 65 private String hmacname = "HMac-SHA512"; private String algorithm = "AES"; 67 private String file; private String keyfile; 70 private StringBuffer input; private StringBuffer output; 73 public static int getScope(String scope) { 74 int ret = PageContext.PAGE_SCOPE; 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 } 88 public int doEndTag() throws JspException { 89 if (value != null) { 91 input = value; 93 } else { 94 if (bodyContent == null || bodyContent.getString() == null) { 96 input = new StringBuffer (""); 97 } else { 98 input = new StringBuffer (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 e) { 109 throw new JspException ("JCE Exception: " + e.getMessage(), e); 110 } 111 112 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 w = pageContext.getOut(); 124 w.print(output); 125 } catch (IOException ex) { 126 throw new JspException (ex.getMessage(), ex); 127 } 128 } 129 130 input = null; 132 133 return EVAL_PAGE; 134 } 136 public void release() { 137 Clean.blank(value); 139 Clean.blank(passphrase); 140 Clean.blank(input); 141 Clean.blank(output); 142 143 super.release(); 144 } 146 153 public void setValue(StringBuffer value) { 154 this.value = value; 155 } 156 157 public StringBuffer getValue() { 158 return value; 159 } 160 161 167 public void setVar(String var) { 168 this.var = var; 169 } 170 171 public String getVar() { 172 return var; 173 } 174 175 181 public void setScope(String scope) { 182 this.scope = getScope(scope); 183 } 184 185 191 public void setHmacname(String hmacname) { 192 this.hmacname = hmacname; 193 } 194 195 201 public void setFile(String file) { 202 this.file = file; 203 } 204 205 211 public void setKeyfile(String keyfile) { 212 this.keyfile = keyfile; 213 } 214 215 222 public void setPassphrase(StringBuffer passphrase) { 223 this.passphrase = passphrase; 224 } 225 226 232 public void setAlgorithm(String algorithm) { 233 this.algorithm = algorithm; 234 } 235 } | Popular Tags |