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 156 157 158 159 160 161 162 163 164 165 166
/* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. */ /* * $Id: DOMSHA1DigestMethod.java,v 1.12 2005/05/10 18:15:34 mullan Exp $ */ package org.jcp.xml.dsig.internal.dom; import javax.xml.crypto.*; import javax.xml.crypto.dsig.*; import javax.xml.crypto.dsig.spec.DigestMethodParameterSpec; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.spec.AlgorithmParameterSpec; import java.util.logging.Level; import java.util.logging.Logger; import org.w3c.dom.Element; /** * DOM-based implementation of DigestMethod for the SHA1, SHA256 and * SHA512 algorithms. * * @author Sean Mullan */ public abstract class DOMSHADigestMethod extends DOMDigestMethod { static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom"); /** * Creates a <code>DOMSHADigestMethod</code>. */ protected DOMSHADigestMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { super(params); } /** * Creates a <code>DOMSHADigestMethod</code> from an element. * * @param dmElem a DigestMethod element */ protected DOMSHADigestMethod(Element dmElem) throws MarshalException { super(dmElem); } protected void checkParams(DigestMethodParameterSpec params) throws InvalidAlgorithmParameterException { if (params != null) { throw new InvalidAlgorithmParameterException("no parameters " + "should be specified for the " + getName() + " DigestMethod algorithm"); } } protected DigestMethodParameterSpec unmarshalParams(Element paramsElem) throws MarshalException { throw new MarshalException("no parameters should " + "be specified for the " + getName() + " DigestMethod algorithm"); } protected void marshalParams(Element parent, String dsPrefix) throws MarshalException { // should never get invoked throw new MarshalException("no parameters should " + "be specified for the " + getName() + " DigestMethod algorithm"); } /** * Returns the name of the DigestMethod algorithm. */ abstract String getName(); /** * Returns a SHA1 DigestMethod. */ static final DOMSHADigestMethod SHA1(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { return new DOMSHA1DigestMethod(params); } static final DOMSHADigestMethod SHA1(Element dmElem) throws MarshalException { return new DOMSHA1DigestMethod(dmElem); } /** * Returns a SHA256 DigestMethod. */ static final DOMSHADigestMethod SHA256(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { return new DOMSHA256DigestMethod(params); } static final DOMSHADigestMethod SHA256(Element dmElem) throws MarshalException { return new DOMSHA256DigestMethod(dmElem); } /** * Returns a SHA512 DigestMethod. */ static final DOMSHADigestMethod SHA512(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { return new DOMSHA512DigestMethod(params); } static final DOMSHADigestMethod SHA512(Element dmElem) throws MarshalException { return new DOMSHA512DigestMethod(dmElem); } private static final class DOMSHA1DigestMethod extends DOMSHADigestMethod { DOMSHA1DigestMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { super(params); } DOMSHA1DigestMethod(Element dmElem) throws MarshalException { super(dmElem); } public String getAlgorithm() { return DigestMethod.SHA1; } String getMessageDigestAlgorithm() { return "SHA"; } String getName() { return "SHA1"; } } private static final class DOMSHA256DigestMethod extends DOMSHADigestMethod{ DOMSHA256DigestMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { super(params); } DOMSHA256DigestMethod(Element dmElem) throws MarshalException { super(dmElem); } public String getAlgorithm() { return DigestMethod.SHA256; } String getMessageDigestAlgorithm() { return "SHA-256"; } String getName() { return "SHA256"; } } private static final class DOMSHA512DigestMethod extends DOMSHADigestMethod{ DOMSHA512DigestMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException { super(params); } DOMSHA512DigestMethod(Element dmElem) throws MarshalException { super(dmElem); } public String getAlgorithm() { return DigestMethod.SHA512; } String getMessageDigestAlgorithm() { return "SHA-512"; } String getName() { return "SHA512"; } } }