messagedigest.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 540 行 · 第 1/2 页
JAVA
540 行
/* * @(#)MessageDigest.java 1.78 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.security;import java.util.*;import java.lang.*;import java.io.IOException;import java.io.ByteArrayOutputStream;import java.io.PrintStream;import java.io.InputStream;import java.io.ByteArrayInputStream;/** * This MessageDigest class provides applications the functionality of a * message digest algorithm, such as MD5 or SHA. * Message digests are secure one-way hash functions that take arbitrary-sized * data and output a fixed-length hash value. * * <p>A MessageDigest object starts out initialized. The data is * processed through it using the {@link #update(byte) update} * methods. At any point {@link #reset() reset} can be called * to reset the digest. Once all the data to be updated has been * updated, one of the {@link #digest() digest} methods should * be called to complete the hash computation. * * <p>The <code>digest</code> method can be called once for a given number * of updates. After <code>digest</code> has been called, the MessageDigest * object is reset to its initialized state. * * <p>Implementations are free to implement the Cloneable interface. * Client applications can test cloneability by attempting cloning * and catching the CloneNotSupportedException: <p> ** <pre>* MessageDigest md = MessageDigest.getInstance("SHA");** try {* md.update(toChapter1);* MessageDigest tc1 = md.clone();* byte[] toChapter1Digest = tc1.digest();* md.update(toChapter2);* ...etc.* } catch (CloneNotSupportedException cnse) {* throw new DigestException("couldn't make digest of partial content");* }* </pre> * * <p>Note that if a given implementation is not cloneable, it is * still possible to compute intermediate digests by instantiating * several instances, if the number of digests is known in advance. * * <p>Note that this class is abstract and extends from * <code>MessageDigestSpi</code> for historical reasons. * Application developers should only take notice of the methods defined in * this <code>MessageDigest</code> class; all the methods in * the superclass are intended for cryptographic service providers who wish to * supply their own implementations of message digest algorithms. * * @author Benjamin Renaud * * @version 1.71, 02/02/00 * * @see DigestInputStream * @see DigestOutputStream */public abstract class MessageDigest extends MessageDigestSpi { private String algorithm; // The state of this digest private static final int INITIAL = 0; private static final int IN_PROGRESS = 1; private int state = INITIAL; // The provider private Provider provider; /** * Creates a message digest with the specified algorithm name. * * @param algorithm the standard name of the digest algorithm. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard algorithm names. */ protected MessageDigest(String algorithm) { this.algorithm = algorithm; } /** * Generates a MessageDigest object that implements the specified digest * algorithm. If the default provider package * provides an implementation of the requested digest algorithm, * an instance of MessageDigest containing that implementation is returned. * If the algorithm is not available in the default * package, other packages are searched. * * @param algorithm the name of the algorithm requested. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard algorithm names. * * @return a Message Digest object implementing the specified * algorithm. * * @exception NoSuchAlgorithmException if the algorithm is * not available in the caller's environment. */ public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException { try { Object[] objs = Security.getImpl(algorithm, "MessageDigest", (String)null); if (objs[0] instanceof MessageDigest) { MessageDigest md = (MessageDigest)objs[0]; md.provider = (Provider)objs[1]; return md; } else { MessageDigest delegate = new Delegate((MessageDigestSpi)objs[0], algorithm); delegate.provider = (Provider)objs[1]; return delegate; } } catch(NoSuchProviderException e) { throw new NoSuchAlgorithmException(algorithm + " not found"); } } /** * Generates a MessageDigest object implementing the specified * algorithm, as supplied from the specified provider, if such an * algorithm is available from the provider. * * @param algorithm the name of the algorithm requested. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard algorithm names. * * @param provider the name of the provider. * * @return a Message Digest object implementing the specified * algorithm. * * @exception NoSuchAlgorithmException if the algorithm is * not available in the package supplied by the requested * provider. * * @exception NoSuchProviderException if the provider is not * available in the environment. * * @exception IllegalArgumentException if the provider name is null * or empty. * * @see Provider */ public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { if (provider == null || provider.length() == 0) throw new IllegalArgumentException("missing provider"); Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider); if (objs[0] instanceof MessageDigest) { MessageDigest md = (MessageDigest)objs[0]; md.provider = (Provider)objs[1]; return md; } else { MessageDigest delegate = new Delegate((MessageDigestSpi)objs[0], algorithm); delegate.provider = (Provider)objs[1]; return delegate; } } /** * Generates a MessageDigest object implementing the specified * algorithm, as supplied from the specified provider, if such an * algorithm is available from the provider. Note: the * <code>provider</code> doesn't have to be registered. * * @param algorithm the name of the algorithm requested. * See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard algorithm names. * * @param provider the provider. * * @return a Message Digest object implementing the specified * algorithm. * * @exception NoSuchAlgorithmException if the algorithm is * not available in the package supplied by the requested * provider. * * @exception IllegalArgumentException if the <code>provider</code> is * null. * * @see Provider * * @since 1.4 */ public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { if (provider == null) throw new IllegalArgumentException("missing provider"); Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider); if (objs[0] instanceof MessageDigest) { MessageDigest md = (MessageDigest)objs[0]; md.provider = (Provider)objs[1]; return md; } else { MessageDigest delegate = new Delegate((MessageDigestSpi)objs[0], algorithm); delegate.provider = (Provider)objs[1]; return delegate; } } /** * Returns the provider of this message digest object. * * @return the provider of this message digest object */ public final Provider getProvider() { return this.provider; } /** * Updates the digest using the specified byte. * * @param input the byte with which to update the digest. */ public void update(byte input) { engineUpdate(input); state = IN_PROGRESS; } /** * Updates the digest using the specified array of bytes, starting
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?