KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > algorithms > MessageDigestAlgorithm


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.algorithms;
18
19
20
21 import java.security.MessageDigest JavaDoc;
22 import java.security.NoSuchProviderException JavaDoc;
23
24 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
25 import com.sun.org.apache.xml.internal.security.utils.Constants;
26 import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants;
27 import org.w3c.dom.Document JavaDoc;
28
29
30 /**
31  * Digest Message wrapper & selector class.
32  *
33  * <pre>
34  * MessageDigestAlgorithm.getInstance()
35  * </pre>
36  *
37  */

38 public class MessageDigestAlgorithm extends Algorithm {
39
40    /** {@link java.util.logging} logging facility */
41     static java.util.logging.Logger JavaDoc log =
42         java.util.logging.Logger.getLogger(
43                     MessageDigestAlgorithm.class.getName());
44
45     /** Message Digest - NOT RECOMMENDED MD5*/
46    public static final String JavaDoc ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5 = Constants.MoreAlgorithmsSpecNS + "md5";
47    /** Digest - Required SHA1*/
48    public static final String JavaDoc ALGO_ID_DIGEST_SHA1 = Constants.SignatureSpecNS + "sha1";
49    /** Message Digest - RECOMMENDED SHA256*/
50    public static final String JavaDoc ALGO_ID_DIGEST_SHA256 = EncryptionConstants.EncryptionSpecNS + "sha256";
51    /** Message Digest - OPTIONAL SHA384*/
52    public static final String JavaDoc ALGO_ID_DIGEST_SHA384 = Constants.MoreAlgorithmsSpecNS + "sha384";
53    /** Message Digest - OPTIONAL SHA512*/
54    public static final String JavaDoc ALGO_ID_DIGEST_SHA512 = EncryptionConstants.EncryptionSpecNS + "sha512";
55    /** Message Digest - OPTIONAL RIPEMD-160*/
56    public static final String JavaDoc ALGO_ID_DIGEST_RIPEMD160 = EncryptionConstants.EncryptionSpecNS + "ripemd160";
57
58    /** Field algorithm stores the actual {@link java.security.MessageDigest} */
59    java.security.MessageDigest JavaDoc algorithm = null;
60
61    /**
62     * Constructor for the brave who pass their own message digest algorithms and the corresponding URI.
63     * @param doc
64     * @param messageDigest
65     * @param algorithmURI
66     */

67    private MessageDigestAlgorithm(Document JavaDoc doc, MessageDigest JavaDoc messageDigest,
68                                   String JavaDoc algorithmURI) {
69
70       super(doc, algorithmURI);
71
72       this.algorithm = messageDigest;
73    }
74
75    /**
76     * Factory method for constructing a message digest algorithm by name.
77     *
78     * @param doc
79     * @param algorithmURI
80     * @return The MessageDigestAlgorithm element to attach in document and to digest
81     * @throws XMLSignatureException
82     */

83    public static MessageDigestAlgorithm getInstance(
84            Document JavaDoc doc, String JavaDoc algorithmURI) throws XMLSignatureException {
85
86       String JavaDoc algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
87
88       if (algorithmID == null) {
89           Object JavaDoc[] exArgs = { algorithmURI };
90           throw new XMLSignatureException("algorithms.NoSuchMap", exArgs);
91       }
92
93       MessageDigest JavaDoc md;
94       String JavaDoc provider=JCEMapper.getProviderId();
95       try {
96          if (provider==null) {
97             md = MessageDigest.getInstance(algorithmID);
98          } else {
99             md = MessageDigest.getInstance(algorithmID,provider);
100          }
101       } catch (java.security.NoSuchAlgorithmException JavaDoc ex) {
102          Object JavaDoc[] exArgs = { algorithmID,
103                              ex.getLocalizedMessage() };
104
105          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
106       } catch (NoSuchProviderException JavaDoc ex) {
107         Object JavaDoc[] exArgs = { algorithmID,
108                             ex.getLocalizedMessage() };
109         
110         throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
111     }
112       return new MessageDigestAlgorithm(doc, md, algorithmURI);
113    }
114
115    /**
116     * Returns the actual {@link java.security.MessageDigest} algorithm object
117     *
118     * @return the actual {@link java.security.MessageDigest} algorithm object
119     */

120    public java.security.MessageDigest JavaDoc getAlgorithm() {
121       return this.algorithm;
122    }
123
124    /**
125     * Proxy method for {@link java.security.MessageDigest#isEqual}
126     * which is executed on the internal {@link java.security.MessageDigest} object.
127     *
128     * @param digesta
129     * @param digestb
130     * @return the result of the {@link java.security.MessageDigest#isEqual} method
131     */

132    public static boolean isEqual(byte[] digesta, byte[] digestb) {
133       return java.security.MessageDigest.isEqual(digesta, digestb);
134    }
135
136    /**
137     * Proxy method for {@link java.security.MessageDigest#digest()}
138     * which is executed on the internal {@link java.security.MessageDigest} object.
139     *
140     * @return the result of the {@link java.security.MessageDigest#digest()} method
141     */

142    public byte[] digest() {
143       return this.algorithm.digest();
144    }
145
146    /**
147     * Proxy method for {@link java.security.MessageDigest#digest(byte[])}
148     * which is executed on the internal {@link java.security.MessageDigest} object.
149     *
150     * @param input
151     * @return the result of the {@link java.security.MessageDigest#digest(byte[])} method
152     */

153    public byte[] digest(byte input[]) {
154       return this.algorithm.digest(input);
155    }
156
157    /**
158     * Proxy method for {@link java.security.MessageDigest#digest(byte[], int, int)}
159     * which is executed on the internal {@link java.security.MessageDigest} object.
160     *
161     * @param buf
162     * @param offset
163     * @param len
164     * @return the result of the {@link java.security.MessageDigest#digest(byte[], int, int)} method
165     * @throws java.security.DigestException
166     */

167    public int digest(byte buf[], int offset, int len)
168            throws java.security.DigestException JavaDoc {
169       return this.algorithm.digest(buf, offset, len);
170    }
171
172    /**
173     * Proxy method for {@link java.security.MessageDigest#getAlgorithm}
174     * which is executed on the internal {@link java.security.MessageDigest} object.
175     *
176     * @return the result of the {@link java.security.MessageDigest#getAlgorithm} method
177     */

178    public String JavaDoc getJCEAlgorithmString() {
179       return this.algorithm.getAlgorithm();
180    }
181
182    /**
183     * Proxy method for {@link java.security.MessageDigest#getProvider}
184     * which is executed on the internal {@link java.security.MessageDigest} object.
185     *
186     * @return the result of the {@link java.security.MessageDigest#getProvider} method
187     */

188    public java.security.Provider JavaDoc getJCEProvider() {
189       return this.algorithm.getProvider();
190    }
191
192    /**
193     * Proxy method for {@link java.security.MessageDigest#getDigestLength}
194     * which is executed on the internal {@link java.security.MessageDigest} object.
195     *
196     * @return the result of the {@link java.security.MessageDigest#getDigestLength} method
197     */

198    public int getDigestLength() {
199       return this.algorithm.getDigestLength();
200    }
201
202    /**
203     * Proxy method for {@link java.security.MessageDigest#reset}
204     * which is executed on the internal {@link java.security.MessageDigest} object.
205     *
206     */

207    public void reset() {
208       this.algorithm.reset();
209    }
210
211    /**
212     * Proxy method for {@link java.security.MessageDigest#update(byte[])}
213     * which is executed on the internal {@link java.security.MessageDigest} object.
214     *
215     * @param input
216     */

217    public void update(byte[] input) {
218       this.algorithm.update(input);
219    }
220
221    /**
222     * Proxy method for {@link java.security.MessageDigest#update(byte)}
223     * which is executed on the internal {@link java.security.MessageDigest} object.
224     *
225     * @param input
226     */

227    public void update(byte input) {
228       this.algorithm.update(input);
229    }
230
231    /**
232     * Proxy method for {@link java.security.MessageDigest#update(byte[], int, int)}
233     * which is executed on the internal {@link java.security.MessageDigest} object.
234     *
235     * @param buf
236     * @param offset
237     * @param len
238     */

239    public void update(byte buf[], int offset, int len) {
240       this.algorithm.update(buf, offset, len);
241    }
242
243    /** @inheritDoc */
244    public String JavaDoc getBaseNamespace() {
245       return Constants.SignatureSpecNS;
246    }
247
248    /** @inheritDoc */
249    public String JavaDoc getBaseLocalName() {
250       return Constants._TAG_DIGESTMETHOD;
251    }
252 }
253
Popular Tags