API Overview API Index Package Overview Direct link to this page
JDK 1.6
  org.jcp.xml.dsig.internal.dom. DOMDigestMethod View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */
/*
 * $Id: DOMDigestMethod.java,v 1.17 2005/05/10 18:15:32 mullan Exp $
 */
package org.jcp.xml.dsig.internal.dom;

import javax.xml.crypto.*;
import javax.xml.crypto.dom.DOMCryptoContext;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.spec.DigestMethodParameterSpec;

import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * DOM-based abstract implementation of DigestMethod.
 *
 * @author Sean Mullan
 */
public abstract class DOMDigestMethod extends DOMStructure 
    implements DigestMethod {

    private DigestMethodParameterSpec params;

    /**
     * Creates a <code>DOMDigestMethod</code>.
     *
     * @param params the algorithm-specific params (may be <code>null</code>)
     * @throws InvalidAlgorithmParameterException if the parameters are not
     *    appropriate for this digest method
     */
    protected DOMDigestMethod(AlgorithmParameterSpec params)
	throws InvalidAlgorithmParameterException {
	if (params != null && !(params instanceof DigestMethodParameterSpec)) {
	    throw new InvalidAlgorithmParameterException
		("params must be of type DigestMethodParameterSpec");
	}
	checkParams((DigestMethodParameterSpec) params);
	this.params = (DigestMethodParameterSpec) params;
    }

    /**
     * Creates a <code>DOMDigestMethod</code> from an element. This constructor
     * invokes the abstract {@link #unmarshalParams unmarshalParams} method to
     * unmarshal any algorithm-specific input parameters.
     *
     * @param dmElem a DigestMethod element
     */
    protected DOMDigestMethod(Element dmElem) throws MarshalException {
	Element paramsElem = DOMUtils.getFirstChildElement(dmElem);
	if (paramsElem != null) {
	    params = unmarshalParams(paramsElem);
	}
	try {
	    checkParams(params);
	} catch (InvalidAlgorithmParameterException iape) {
	    throw new MarshalException(iape);
	}
    }

    static DigestMethod unmarshal(Element dmElem) throws MarshalException {
        String alg = DOMUtils.getAttributeValue(dmElem, "Algorithm");
        if (alg.equals(DigestMethod.SHA1)) {
            return DOMSHADigestMethod.SHA1(dmElem);
        } else if (alg.equals(DigestMethod.SHA256)) {
            return DOMSHADigestMethod.SHA256(dmElem);
        } else if (alg.equals(DigestMethod.SHA512)) {
            return DOMSHADigestMethod.SHA512(dmElem);
        } else {
            throw new MarshalException("unsupported digest algorithm: " + alg);
        }
    }

    /**
     * Checks if the specified parameters are valid for this algorithm.
     *
     * @param params the algorithm-specific params (may be <code>null</code>)
     * @throws InvalidAlgorithmParameterException if the parameters are not
     *    appropriate for this digest method
     */
    protected abstract void checkParams(DigestMethodParameterSpec params) 
	throws InvalidAlgorithmParameterException;

    public final AlgorithmParameterSpec getParameterSpec() {
	return params;
    }

    /**
     * Unmarshals <code>DigestMethodParameterSpec</code> from the specified 
     * <code>Element</code>. Subclasses should implement this to unmarshal
     * the algorithm-specific parameters.
     *
     * @param paramsElem the <code>Element</code> holding the input params
     * @return the algorithm-specific <code>DigestMethodParameterSpec</code>
     * @throws MarshalException if the parameters cannot be unmarshalled
     */
    protected abstract DigestMethodParameterSpec 
	unmarshalParams(Element paramsElem) throws MarshalException;

    /**
     * This method invokes the abstract {@link #marshalParams marshalParams} 
     * method to marshal any algorithm-specific parameters.
     */
    public void marshal(Node parent, String prefix, DOMCryptoContext context) 
	throws MarshalException {
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);

        Element dmElem = DOMUtils.createElement
	    (ownerDoc, "DigestMethod", XMLSignature.XMLNS, prefix);
        DOMUtils.setAttribute(dmElem, "Algorithm", getAlgorithm());

        if (params != null) {
	    marshalParams(dmElem, prefix);
        }

        parent.appendChild(dmElem);
    }

    public boolean equals(Object o) {
	if (this == o) {
            return true;
	}

        if (!(o instanceof DigestMethod)) {
            return false;
	}
        DigestMethod odm = (DigestMethod) o;

	boolean paramsEqual = (params == null ? odm.getParameterSpec() == null :
	    params.equals(odm.getParameterSpec()));

	return (getAlgorithm().equals(odm.getAlgorithm()) && paramsEqual);
    }

    /**
     * Marshals the algorithm-specific parameters to an Element and
     * appends it to the specified parent element.
     *
     * @param parent the parent element to append the parameters to
     * @param the namespace prefix to use
     * @throws MarshalException if the parameters cannot be marshalled
     */
    protected abstract void marshalParams(Element parent, String prefix)
	throws MarshalException;

    /**
     * Returns the MessageDigest standard algorithm name.
     */
    abstract String getMessageDigestAlgorithm();
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar