blob: 805d061a2e6dd5b6cfa37a67f2b5d517a059f1a5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security.algorithms;
22
23
24
25import java.security.MessageDigest;
26import java.security.NoSuchProviderException;
27
28import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
29import com.sun.org.apache.xml.internal.security.utils.Constants;
30import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants;
31import org.w3c.dom.Document;
32
33
34/**
35 * Digest Message wrapper & selector class.
36 *
37 * <pre>
38 * MessageDigestAlgorithm.getInstance()
39 * </pre>
40 *
41 */
42public class MessageDigestAlgorithm extends Algorithm {
43
44 /** {@link java.util.logging} logging facility */
45 static java.util.logging.Logger log =
46 java.util.logging.Logger.getLogger(
47 MessageDigestAlgorithm.class.getName());
48
49 /** Message Digest - NOT RECOMMENDED MD5*/
50 public static final String ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5 = Constants.MoreAlgorithmsSpecNS + "md5";
51 /** Digest - Required SHA1*/
52 public static final String ALGO_ID_DIGEST_SHA1 = Constants.SignatureSpecNS + "sha1";
53 /** Message Digest - RECOMMENDED SHA256*/
54 public static final String ALGO_ID_DIGEST_SHA256 = EncryptionConstants.EncryptionSpecNS + "sha256";
55 /** Message Digest - OPTIONAL SHA384*/
56 public static final String ALGO_ID_DIGEST_SHA384 = Constants.MoreAlgorithmsSpecNS + "sha384";
57 /** Message Digest - OPTIONAL SHA512*/
58 public static final String ALGO_ID_DIGEST_SHA512 = EncryptionConstants.EncryptionSpecNS + "sha512";
59 /** Message Digest - OPTIONAL RIPEMD-160*/
60 public static final String ALGO_ID_DIGEST_RIPEMD160 = EncryptionConstants.EncryptionSpecNS + "ripemd160";
61
62 /** Field algorithm stores the actual {@link java.security.MessageDigest} */
63 java.security.MessageDigest algorithm = null;
64
65 /**
66 * Constructor for the brave who pass their own message digest algorithms and the corresponding URI.
67 * @param doc
68 * @param messageDigest
69 * @param algorithmURI
70 */
71 private MessageDigestAlgorithm(Document doc, MessageDigest messageDigest,
72 String algorithmURI) {
73
74 super(doc, algorithmURI);
75
76 this.algorithm = messageDigest;
77 }
78
79 /**
80 * Factory method for constructing a message digest algorithm by name.
81 *
82 * @param doc
83 * @param algorithmURI
84 * @return The MessageDigestAlgorithm element to attach in document and to digest
85 * @throws XMLSignatureException
86 */
87 public static MessageDigestAlgorithm getInstance(
88 Document doc, String algorithmURI) throws XMLSignatureException {
89
90 String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
91
92 if (algorithmID == null) {
93 Object[] exArgs = { algorithmURI };
94 throw new XMLSignatureException("algorithms.NoSuchMap", exArgs);
95 }
96
97 MessageDigest md;
98 String provider=JCEMapper.getProviderId();
99 try {
100 if (provider==null) {
101 md = MessageDigest.getInstance(algorithmID);
102 } else {
103 md = MessageDigest.getInstance(algorithmID,provider);
104 }
105 } catch (java.security.NoSuchAlgorithmException ex) {
106 Object[] exArgs = { algorithmID,
107 ex.getLocalizedMessage() };
108
109 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
110 } catch (NoSuchProviderException ex) {
111 Object[] exArgs = { algorithmID,
112 ex.getLocalizedMessage() };
113
114 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
115 }
116 return new MessageDigestAlgorithm(doc, md, algorithmURI);
117 }
118
119 /**
120 * Returns the actual {@link java.security.MessageDigest} algorithm object
121 *
122 * @return the actual {@link java.security.MessageDigest} algorithm object
123 */
124 public java.security.MessageDigest getAlgorithm() {
125 return this.algorithm;
126 }
127
128 /**
129 * Proxy method for {@link java.security.MessageDigest#isEqual}
130 * which is executed on the internal {@link java.security.MessageDigest} object.
131 *
132 * @param digesta
133 * @param digestb
134 * @return the result of the {@link java.security.MessageDigest#isEqual} method
135 */
136 public static boolean isEqual(byte[] digesta, byte[] digestb) {
137 return java.security.MessageDigest.isEqual(digesta, digestb);
138 }
139
140 /**
141 * Proxy method for {@link java.security.MessageDigest#digest()}
142 * which is executed on the internal {@link java.security.MessageDigest} object.
143 *
144 * @return the result of the {@link java.security.MessageDigest#digest()} method
145 */
146 public byte[] digest() {
147 return this.algorithm.digest();
148 }
149
150 /**
151 * Proxy method for {@link java.security.MessageDigest#digest(byte[])}
152 * which is executed on the internal {@link java.security.MessageDigest} object.
153 *
154 * @param input
155 * @return the result of the {@link java.security.MessageDigest#digest(byte[])} method
156 */
157 public byte[] digest(byte input[]) {
158 return this.algorithm.digest(input);
159 }
160
161 /**
162 * Proxy method for {@link java.security.MessageDigest#digest(byte[], int, int)}
163 * which is executed on the internal {@link java.security.MessageDigest} object.
164 *
165 * @param buf
166 * @param offset
167 * @param len
168 * @return the result of the {@link java.security.MessageDigest#digest(byte[], int, int)} method
169 * @throws java.security.DigestException
170 */
171 public int digest(byte buf[], int offset, int len)
172 throws java.security.DigestException {
173 return this.algorithm.digest(buf, offset, len);
174 }
175
176 /**
177 * Proxy method for {@link java.security.MessageDigest#getAlgorithm}
178 * which is executed on the internal {@link java.security.MessageDigest} object.
179 *
180 * @return the result of the {@link java.security.MessageDigest#getAlgorithm} method
181 */
182 public String getJCEAlgorithmString() {
183 return this.algorithm.getAlgorithm();
184 }
185
186 /**
187 * Proxy method for {@link java.security.MessageDigest#getProvider}
188 * which is executed on the internal {@link java.security.MessageDigest} object.
189 *
190 * @return the result of the {@link java.security.MessageDigest#getProvider} method
191 */
192 public java.security.Provider getJCEProvider() {
193 return this.algorithm.getProvider();
194 }
195
196 /**
197 * Proxy method for {@link java.security.MessageDigest#getDigestLength}
198 * which is executed on the internal {@link java.security.MessageDigest} object.
199 *
200 * @return the result of the {@link java.security.MessageDigest#getDigestLength} method
201 */
202 public int getDigestLength() {
203 return this.algorithm.getDigestLength();
204 }
205
206 /**
207 * Proxy method for {@link java.security.MessageDigest#reset}
208 * which is executed on the internal {@link java.security.MessageDigest} object.
209 *
210 */
211 public void reset() {
212 this.algorithm.reset();
213 }
214
215 /**
216 * Proxy method for {@link java.security.MessageDigest#update(byte[])}
217 * which is executed on the internal {@link java.security.MessageDigest} object.
218 *
219 * @param input
220 */
221 public void update(byte[] input) {
222 this.algorithm.update(input);
223 }
224
225 /**
226 * Proxy method for {@link java.security.MessageDigest#update(byte)}
227 * which is executed on the internal {@link java.security.MessageDigest} object.
228 *
229 * @param input
230 */
231 public void update(byte input) {
232 this.algorithm.update(input);
233 }
234
235 /**
236 * Proxy method for {@link java.security.MessageDigest#update(byte[], int, int)}
237 * which is executed on the internal {@link java.security.MessageDigest} object.
238 *
239 * @param buf
240 * @param offset
241 * @param len
242 */
243 public void update(byte buf[], int offset, int len) {
244 this.algorithm.update(buf, offset, len);
245 }
246
247 /** @inheritDoc */
248 public String getBaseNamespace() {
249 return Constants.SignatureSpecNS;
250 }
251
252 /** @inheritDoc */
253 public String getBaseLocalName() {
254 return Constants._TAG_DIGESTMETHOD;
255 }
256}