org.jcp.xml.dsig.internal.dom.DOMHMACSignatureMethod Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal.dom;
/* */
/* */ import java.security.InvalidAlgorithmParameterException;
/* */ import java.security.InvalidKeyException;
/* */ import java.security.Key;
/* */ import java.security.SignatureException;
/* */ import java.security.spec.AlgorithmParameterSpec;
/* */ import java.util.logging.Level;
/* */ import java.util.logging.Logger;
/* */ import javax.xml.crypto.MarshalException;
/* */ import javax.xml.crypto.dsig.XMLSignContext;
/* */ import javax.xml.crypto.dsig.XMLSignatureException;
/* */ import javax.xml.crypto.dsig.XMLValidateContext;
/* */ import javax.xml.crypto.dsig.spec.HMACParameterSpec;
/* */ import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec;
/* */ import org.jcp.xml.dsig.internal.HmacSHA1;
/* */ import org.jcp.xml.dsig.internal.MacOutputStream;
/* */ import org.w3c.dom.Document;
/* */ import org.w3c.dom.Element;
/* */ import org.w3c.dom.Node;
/* */
/* */ public final class DOMHMACSignatureMethod extends DOMSignatureMethod
/* */ {
/* 34 */ private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
/* 35 */ private HmacSHA1 hmac = new HmacSHA1();
/* */ private int outputLength;
/* */
/* */ public DOMHMACSignatureMethod(AlgorithmParameterSpec params)
/* */ throws InvalidAlgorithmParameterException
/* */ {
/* 46 */ super("http://www.w3.org/2000/09/xmldsig#hmac-sha1", params);
/* */ }
/* */
/* */ public DOMHMACSignatureMethod(Element smElem)
/* */ throws MarshalException
/* */ {
/* 55 */ super(smElem);
/* */ }
/* */
/* */ protected void checkParams(SignatureMethodParameterSpec params) throws InvalidAlgorithmParameterException
/* */ {
/* 60 */ if (params != null) {
/* 61 */ if (!(params instanceof HMACParameterSpec)) {
/* 62 */ throw new InvalidAlgorithmParameterException("params must be of type HMACParameterSpec");
/* */ }
/* */
/* 65 */ this.outputLength = ((HMACParameterSpec)params).getOutputLength();
/* 66 */ if (log.isLoggable(Level.FINE)) {
/* 67 */ log.log(Level.FINE, "Setting outputLength from HMACParameterSpec to: " + this.outputLength);
/* */ }
/* */ }
/* */ else
/* */ {
/* 72 */ this.outputLength = -1;
/* */ }
/* */ }
/* */
/* */ protected SignatureMethodParameterSpec unmarshalParams(Element paramsElem) throws MarshalException
/* */ {
/* 78 */ this.outputLength = new Integer(paramsElem.getFirstChild().getNodeValue()).intValue();
/* */
/* 80 */ if (log.isLoggable(Level.FINE)) {
/* 81 */ log.log(Level.FINE, "unmarshalled outputLength: " + this.outputLength);
/* */ }
/* 83 */ return new HMACParameterSpec(this.outputLength);
/* */ }
/* */
/* */ protected void marshalParams(Element parent, String prefix)
/* */ throws MarshalException
/* */ {
/* 89 */ Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/* 90 */ Element hmacElem = DOMUtils.createElement(ownerDoc, "HMACOutputLength", "http://www.w3.org/2000/09/xmldsig#", prefix);
/* */
/* 92 */ hmacElem.appendChild(ownerDoc.createTextNode(String.valueOf(this.outputLength)));
/* */
/* 95 */ parent.appendChild(hmacElem);
/* */ }
/* */
/* */ public boolean verify(Key key, DOMSignedInfo si, byte[] sig, XMLValidateContext context)
/* */ throws InvalidKeyException, SignatureException, XMLSignatureException
/* */ {
/* 101 */ if ((key == null) || (si == null) || (sig == null)) {
/* 102 */ throw new NullPointerException("key, signedinfo or signature data can't be null");
/* */ }
/* */
/* 105 */ if (log.isLoggable(Level.FINE)) {
/* 106 */ log.log(Level.FINE, "outputLength = " + this.outputLength);
/* */ }
/* 108 */ this.hmac.init(key, this.outputLength);
/* 109 */ si.canonicalize(context, new MacOutputStream(this.hmac));
/* 110 */ return this.hmac.verify(sig);
/* */ }
/* */
/* */ public byte[] sign(Key key, DOMSignedInfo si, XMLSignContext context) throws InvalidKeyException, XMLSignatureException
/* */ {
/* 115 */ if ((key == null) || (si == null)) {
/* 116 */ throw new NullPointerException();
/* */ }
/* 118 */ this.hmac.init(key, this.outputLength);
/* 119 */ si.canonicalize(context, new MacOutputStream(this.hmac));
/* */ try
/* */ {
/* 122 */ return this.hmac.sign();
/* */ }
/* */ catch (SignatureException se) {
/* 125 */ throw new RuntimeException(se.getMessage());
/* */ }
/* */ }
/* */
/* */ public boolean paramsEqual(AlgorithmParameterSpec spec) {
/* 130 */ if (getParameterSpec() == spec) {
/* 131 */ return true;
/* */ }
/* 133 */ if (!(spec instanceof HMACParameterSpec)) {
/* 134 */ return false;
/* */ }
/* 136 */ HMACParameterSpec ospec = (HMACParameterSpec)spec;
/* */
/* 138 */ return this.outputLength == ospec.getOutputLength();
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.dom.DOMHMACSignatureMethod
* JD-Core Version: 0.6.2
*/