📄 hmac.java
字号:
/*
Name: HMAC.java
Licensing: LGPL
API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
Provider: Bouncy Castle (http://www.bouncycastle.org)
Disclaimer:
COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
*/
package net.sourceforge.jcetaglib.taglib.crypto;
import net.sourceforge.jcetaglib.lib.Clean;
import net.sourceforge.jcetaglib.lib.Macs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
/**
* JSP tag used for creating HMAC (message authentication code) of strings and files
*
* @jsp.tag
* name="hmac"
* display-name="HMAC"
* body-content="JSP"
* example="
* <%-- Generates and print the MAC from a file --%>
* <jce:hmac
* 	keyfile=\"c:/jspkey.key\"
* 	passphrase=\"<%= new StringBuffer(\"password\") %>\"
* 	file=\"c:/zipfile.zip\"/>"
*
* description="JSP tag used for creating HMAC (message authentication code) of strings and files"
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version 0.6 (beta) 30-oct-2002
*/
public class HMAC extends BodyTagSupport {
private static final String PAGE = "page";
private static final String REQUEST = "request";
private static final String SESSION = "session";
private static final String APPLICATION = "application";
private StringBuffer value; // tag attribute
private String var; // tag attribute
private int scope = PageContext.PAGE_SCOPE; // tag attribute
private StringBuffer passphrase; // tag attribute
private String hmacname = "HMac-SHA512"; // tag attribute
private String algorithm = "AES";
private String file; // tag attribute
private String keyfile; // tag attribute
private StringBuffer input; // what we'll store in scope:var
private StringBuffer output; // return text
public static int getScope(String scope) {
int ret = PageContext.PAGE_SCOPE; // default
if (REQUEST.equalsIgnoreCase(scope))
ret = PageContext.REQUEST_SCOPE;
else if (SESSION.equalsIgnoreCase(scope))
ret = PageContext.SESSION_SCOPE;
else if (APPLICATION.equalsIgnoreCase(scope))
ret = PageContext.APPLICATION_SCOPE;
else if (PAGE.equalsIgnoreCase(scope))
ret = PageContext.PAGE_SCOPE;
return ret;
} //getScope()
public int doEndTag() throws JspException {
// determine the value by...
if (value != null) {
// ... reading our attribute
input = value;
} else {
// ... retrieving and trimming our body
if (bodyContent == null || bodyContent.getString() == null) {
input = new StringBuffer("");
} else {
input = new StringBuffer(bodyContent.getString().trim());
}
}
try {
if (file != null) {
output = Macs.generateFileMAC(file, keyfile, passphrase, algorithm, hmacname);
} else {
output = Macs.generateMAC(input, keyfile, passphrase, algorithm, hmacname);
}
} catch (Exception e) {
throw new JspException("JCE Exception: " + e.getMessage(), e);
}
// decide what to do with the result
if (var != null) {
if (output != null) {
pageContext.setAttribute(var, output, scope);
}
} else {
if (bodyContent != null) {
bodyContent.clearBody();
}
try {
JspWriter w = pageContext.getOut();
w.print(output);
} catch (IOException ex) {
throw new JspException(ex.getMessage(), ex);
}
}
// Cleanup all sensitive information
input = null;
return EVAL_PAGE;
} // doEndTag()
public void release() {
// Cleanup all sensitive information
Clean.blank(value);
Clean.blank(passphrase);
Clean.blank(input);
Clean.blank(output);
super.release();
} //release()
/**
* @jsp.attribute
* description="Optional attribute, the string to generate the MAC from. The body of the tag will be taken if omitted. "
* type="java.lang.StringBuffer"
* required="false"
* rtexprvalue="true"
*/
public void setValue(StringBuffer value) {
this.value = value;
}
public StringBuffer getValue() {
return value;
}
/**
* @jsp.attribute
* description="Optional attribute, variable to store the MAC. The MAC will be printed if omitted."
* required="false"
* rtexprvalue="false"
*/
public void setVar(String var) {
this.var = var;
}
public String getVar() {
return var;
}
/**
* @jsp.attribute
* description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'."
* required="false"
* rtexprvalue="false"
*/
public void setScope(String scope) {
this.scope = getScope(scope);
}
/**
* @jsp.attribute
* description="The MAC algorithm. See the next paragraph under 'MAC' for supported algorithms. Default is 'HMac-SHA512'."
* required="false"
* rtexprvalue="true"
*/
public void setHmacname(String hmacname) {
this.hmacname = hmacname;
}
/**
* @jsp.attribute
* description="Create a MAC from a file instead of a string"
* required="false"
* rtexprvalue="true"
*/
public void setFile(String file) {
this.file = file;
}
/**
* @jsp.attribute
* description="The symmetric key file(name)"
* required="true"
* rtexprvalue="true"
*/
public void setKeyfile(String keyfile) {
this.keyfile = keyfile;
}
/**
* @jsp.attribute
* description="Keystore passphrase"
* type="java.lang.StringBuffer"
* required="true"
* rtexprvalue="true"
*/
public void setPassphrase(StringBuffer passphrase) {
this.passphrase = passphrase;
}
/**
* @jsp.attribute
* description="The key algorithm. Default is 'AES'"
* required="false"
* rtexprvalue="true"
*/
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -