9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.security;
27
28 import java.util.*;
29 import java.lang.*;
30 import java.io.IOException;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.io.InputStream;
34 import java.io.ByteArrayInputStream;
35 import java.security.InvalidKeyException;
36 import java.nio.ByteBuffer;
37
38 import sun.security.util.Debug;
39 import sun.security.util.MessageDigestSpi2;
40
41 import javax.crypto.SecretKey;
42
43 /**
44 * This MessageDigest class provides applications the functionality of a
45 * message digest algorithm, such as SHA-1 or SHA-256.
46 * Message digests are secure one-way hash functions that take arbitrary-sized
47 * data and output a fixed-length hash value.
48 *
49 * <p>A MessageDigest object starts out initialized. The data is
50 * processed through it using the {@link #update(byte) update}
51 * methods. At any point {@link #reset() reset} can be called
52 * to reset the digest. Once all the data to be updated has been
53 * updated, one of the {@link #digest() digest} methods should
54 * be called to complete the hash computation.
55 *
146 * MessageDigestSpi implementation from the first
147 * Provider that supports the specified algorithm is returned.
148 *
149 * <p> Note that the list of registered providers may be retrieved via
150 * the {@link Security#getProviders() Security.getProviders()} method.
151 *
152 * @implNote
153 * The JDK Reference Implementation additionally uses the
154 * {@code jdk.security.provider.preferred}
155 * {@link Security#getProperty(String) Security} property to determine
156 * the preferred provider order for the specified algorithm. This
157 * may be different than the order of providers returned by
158 * {@link Security#getProviders() Security.getProviders()}.
159 *
160 * @param algorithm the name of the algorithm requested.
161 * See the MessageDigest section in the <a href=
162 * "{@docRoot}/../technotes/guides/security/StandardNames.html#MessageDigest">
163 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
164 * for information about standard algorithm names.
165 *
166 * @return a Message Digest object that implements the specified algorithm.
167 *
168 * @exception NoSuchAlgorithmException if no Provider supports a
169 * MessageDigestSpi implementation for the
170 * specified algorithm.
171 *
172 * @see Provider
173 */
174 public static MessageDigest getInstance(String algorithm)
175 throws NoSuchAlgorithmException {
176 try {
177 MessageDigest md;
178 Object[] objs = Security.getImpl(algorithm, "MessageDigest",
179 (String)null);
180 if (objs[0] instanceof MessageDigest) {
181 md = (MessageDigest)objs[0];
182 } else {
183 md = new Delegate((MessageDigestSpi)objs[0], algorithm);
184 }
185 md.provider = (Provider)objs[1];
186
187 if (!skipDebug && pdebug != null) {
188 pdebug.println("MessageDigest." + algorithm +
189 " algorithm from: " + md.provider.getName());
190 }
191
192 return md;
193
194 } catch(NoSuchProviderException e) {
195 throw new NoSuchAlgorithmException(algorithm + " not found");
199 /**
200 * Returns a MessageDigest object that implements the specified digest
201 * algorithm.
202 *
203 * <p> A new MessageDigest object encapsulating the
204 * MessageDigestSpi implementation from the specified provider
205 * is returned. The specified provider must be registered
206 * in the security provider list.
207 *
208 * <p> Note that the list of registered providers may be retrieved via
209 * the {@link Security#getProviders() Security.getProviders()} method.
210 *
211 * @param algorithm the name of the algorithm requested.
212 * See the MessageDigest section in the <a href=
213 * "{@docRoot}/../technotes/guides/security/StandardNames.html#MessageDigest">
214 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
215 * for information about standard algorithm names.
216 *
217 * @param provider the name of the provider.
218 *
219 * @return a MessageDigest object that implements the specified algorithm.
220 *
221 * @exception NoSuchAlgorithmException if a MessageDigestSpi
222 * implementation for the specified algorithm is not
223 * available from the specified provider.
224 *
225 * @exception NoSuchProviderException if the specified provider is not
226 * registered in the security provider list.
227 *
228 * @exception IllegalArgumentException if the provider name is null
229 * or empty.
230 *
231 * @see Provider
232 */
233 public static MessageDigest getInstance(String algorithm, String provider)
234 throws NoSuchAlgorithmException, NoSuchProviderException
235 {
236 if (provider == null || provider.length() == 0)
237 throw new IllegalArgumentException("missing provider");
238 Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
239 if (objs[0] instanceof MessageDigest) {
240 MessageDigest md = (MessageDigest)objs[0];
241 md.provider = (Provider)objs[1];
242 return md;
243 } else {
244 MessageDigest delegate =
245 new Delegate((MessageDigestSpi)objs[0], algorithm);
246 delegate.provider = (Provider)objs[1];
247 return delegate;
248 }
249 }
250
251 /**
252 * Returns a MessageDigest object that implements the specified digest
253 * algorithm.
254 *
255 * <p> A new MessageDigest object encapsulating the
256 * MessageDigestSpi implementation from the specified Provider
257 * object is returned. Note that the specified Provider object
258 * does not have to be registered in the provider list.
259 *
260 * @param algorithm the name of the algorithm requested.
261 * See the MessageDigest section in the <a href=
262 * "{@docRoot}/../technotes/guides/security/StandardNames.html#MessageDigest">
263 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
264 * for information about standard algorithm names.
265 *
266 * @param provider the provider.
267 *
268 * @return a MessageDigest object that implements the specified algorithm.
269 *
270 * @exception NoSuchAlgorithmException if a MessageDigestSpi
271 * implementation for the specified algorithm is not available
272 * from the specified Provider object.
273 *
274 * @exception IllegalArgumentException if the specified provider is null.
275 *
276 * @see Provider
277 *
278 * @since 1.4
279 */
280 public static MessageDigest getInstance(String algorithm,
281 Provider provider)
282 throws NoSuchAlgorithmException
283 {
284 if (provider == null)
285 throw new IllegalArgumentException("missing provider");
286 Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
287 if (objs[0] instanceof MessageDigest) {
288 MessageDigest md = (MessageDigest)objs[0];
289 md.provider = (Provider)objs[1];
290 return md;
291 } else {
292 MessageDigest delegate =
293 new Delegate((MessageDigestSpi)objs[0], algorithm);
294 delegate.provider = (Provider)objs[1];
295 return delegate;
296 }
297 }
298
299 /**
300 * Returns the provider of this message digest object.
301 *
302 * @return the provider of this message digest object
303 */
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.security;
27
28 import java.util.*;
29 import java.io.ByteArrayOutputStream;
30 import java.io.PrintStream;
31 import java.nio.ByteBuffer;
32
33 import sun.security.util.Debug;
34 import sun.security.util.MessageDigestSpi2;
35
36 import javax.crypto.SecretKey;
37
38 /**
39 * This MessageDigest class provides applications the functionality of a
40 * message digest algorithm, such as SHA-1 or SHA-256.
41 * Message digests are secure one-way hash functions that take arbitrary-sized
42 * data and output a fixed-length hash value.
43 *
44 * <p>A MessageDigest object starts out initialized. The data is
45 * processed through it using the {@link #update(byte) update}
46 * methods. At any point {@link #reset() reset} can be called
47 * to reset the digest. Once all the data to be updated has been
48 * updated, one of the {@link #digest() digest} methods should
49 * be called to complete the hash computation.
50 *
141 * MessageDigestSpi implementation from the first
142 * Provider that supports the specified algorithm is returned.
143 *
144 * <p> Note that the list of registered providers may be retrieved via
145 * the {@link Security#getProviders() Security.getProviders()} method.
146 *
147 * @implNote
148 * The JDK Reference Implementation additionally uses the
149 * {@code jdk.security.provider.preferred}
150 * {@link Security#getProperty(String) Security} property to determine
151 * the preferred provider order for the specified algorithm. This
152 * may be different than the order of providers returned by
153 * {@link Security#getProviders() Security.getProviders()}.
154 *
155 * @param algorithm the name of the algorithm requested.
156 * See the MessageDigest section in the <a href=
157 * "{@docRoot}/../technotes/guides/security/StandardNames.html#MessageDigest">
158 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
159 * for information about standard algorithm names.
160 *
161 * @return a {@code MessageDigest} object that implements the
162 * specified algorithm
163 *
164 * @throws NoSuchAlgorithmException if no {@code Provider} supports a
165 * {@code MessageDigestSpi} implementation for the
166 * specified algorithm
167 *
168 * @throws NullPointerException if {@code algorithm} is {@code null}
169 *
170 * @see Provider
171 */
172 public static MessageDigest getInstance(String algorithm)
173 throws NoSuchAlgorithmException {
174 Objects.requireNonNull(algorithm, "null algorithm name");
175 try {
176 MessageDigest md;
177 Object[] objs = Security.getImpl(algorithm, "MessageDigest",
178 (String)null);
179 if (objs[0] instanceof MessageDigest) {
180 md = (MessageDigest)objs[0];
181 } else {
182 md = new Delegate((MessageDigestSpi)objs[0], algorithm);
183 }
184 md.provider = (Provider)objs[1];
185
186 if (!skipDebug && pdebug != null) {
187 pdebug.println("MessageDigest." + algorithm +
188 " algorithm from: " + md.provider.getName());
189 }
190
191 return md;
192
193 } catch(NoSuchProviderException e) {
194 throw new NoSuchAlgorithmException(algorithm + " not found");
198 /**
199 * Returns a MessageDigest object that implements the specified digest
200 * algorithm.
201 *
202 * <p> A new MessageDigest object encapsulating the
203 * MessageDigestSpi implementation from the specified provider
204 * is returned. The specified provider must be registered
205 * in the security provider list.
206 *
207 * <p> Note that the list of registered providers may be retrieved via
208 * the {@link Security#getProviders() Security.getProviders()} method.
209 *
210 * @param algorithm the name of the algorithm requested.
211 * See the MessageDigest section in the <a href=
212 * "{@docRoot}/../technotes/guides/security/StandardNames.html#MessageDigest">
213 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
214 * for information about standard algorithm names.
215 *
216 * @param provider the name of the provider.
217 *
218 * @return a {@code MessageDigest} object that implements the
219 * specified algorithm
220 *
221 * @throws IllegalArgumentException if the provider name is {@code null}
222 * or empty
223 *
224 * @throws NoSuchAlgorithmException if a {@code MessageDigestSpi}
225 * implementation for the specified algorithm is not
226 * available from the specified provider
227 *
228 * @throws NoSuchProviderException if the specified provider is not
229 * registered in the security provider list
230 *
231 * @throws NullPointerException if {@code algorithm} is {@code null}
232 *
233 * @see Provider
234 */
235 public static MessageDigest getInstance(String algorithm, String provider)
236 throws NoSuchAlgorithmException, NoSuchProviderException
237 {
238 Objects.requireNonNull(algorithm, "null algorithm name");
239 if (provider == null || provider.length() == 0)
240 throw new IllegalArgumentException("missing provider");
241 Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
242 if (objs[0] instanceof MessageDigest) {
243 MessageDigest md = (MessageDigest)objs[0];
244 md.provider = (Provider)objs[1];
245 return md;
246 } else {
247 MessageDigest delegate =
248 new Delegate((MessageDigestSpi)objs[0], algorithm);
249 delegate.provider = (Provider)objs[1];
250 return delegate;
251 }
252 }
253
254 /**
255 * Returns a MessageDigest object that implements the specified digest
256 * algorithm.
257 *
258 * <p> A new MessageDigest object encapsulating the
259 * MessageDigestSpi implementation from the specified Provider
260 * object is returned. Note that the specified Provider object
261 * does not have to be registered in the provider list.
262 *
263 * @param algorithm the name of the algorithm requested.
264 * See the MessageDigest section in the <a href=
265 * "{@docRoot}/../technotes/guides/security/StandardNames.html#MessageDigest">
266 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
267 * for information about standard algorithm names.
268 *
269 * @param provider the provider.
270 *
271 * @return a {@code MessageDigest} object that implements the
272 * specified algorithm
273 *
274 * @throws IllegalArgumentException if the specified provider is
275 * {@code null}
276 *
277 * @throws NoSuchAlgorithmException if a {@code MessageDigestSpi}
278 * implementation for the specified algorithm is not available
279 * from the specified {@code Provider} object
280 *
281 * @throws NullPointerException if {@code algorithm} is {@code null}
282 *
283 * @see Provider
284 *
285 * @since 1.4
286 */
287 public static MessageDigest getInstance(String algorithm,
288 Provider provider)
289 throws NoSuchAlgorithmException
290 {
291 Objects.requireNonNull(algorithm, "null algorithm name");
292 if (provider == null)
293 throw new IllegalArgumentException("missing provider");
294 Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
295 if (objs[0] instanceof MessageDigest) {
296 MessageDigest md = (MessageDigest)objs[0];
297 md.provider = (Provider)objs[1];
298 return md;
299 } else {
300 MessageDigest delegate =
301 new Delegate((MessageDigestSpi)objs[0], algorithm);
302 delegate.provider = (Provider)objs[1];
303 return delegate;
304 }
305 }
306
307 /**
308 * Returns the provider of this message digest object.
309 *
310 * @return the provider of this message digest object
311 */
|