signature.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 568 行 · 第 1/2 页

JAVA
568
字号
/* Signature.java --- Signature Class   Copyright (C) 1999, 2002, 2003, 2004  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.security;import gnu.java.security.Engine;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.security.spec.AlgorithmParameterSpec;/** * <code>Signature</code> is used to provide an interface to digital signature * algorithms. Digital signatures provide authentication and data integrity of * digital data. *  * <p>The GNU provider provides the NIST standard DSA which uses DSA and SHA-1. * It can be specified by SHA/DSA, SHA-1/DSA or its OID. If the RSA signature * algorithm is provided then it could be MD2/RSA. MD5/RSA, or SHA-1/RSA. The * algorithm must be specified because there is no default.</p> *  * <p>Signature provides implementation-independent algorithms which are * requested by the user through the <code>getInstance()<?code> methods. It can * be requested by specifying just the algorithm name or by specifying both the * algorithm name and provider name.</p> *  * <p>The three phases of using <code>Signature</code> are:</p> *  * <ol> *   <li>Initializing: *     <ul> *       <li>It must be initialized with a private key for signing.</li> *       <li>It must be initialized with a public key for verifying.</li> *   </li> *    *   <li>Updating: *   <p>Update the bytes for signing or verifying with calls to update.</p> *   </li> *    *   <li>Signing or Verify the signature on the currently stored bytes by *   calling sign or verify.</li> * </ol> * * @author Mark Benvenuto  (ivymccough@worldnet.att.net) */public abstract class Signature extends SignatureSpi{  /** Service name for signatures. */  private static final String SIGNATURE = "Signature";  /**   * Possible state value which signifies that this instance has not yet been   * initialized.   */  protected static final int UNINITIALIZED = 0;  /**   * Possible state value which signifies that this instance has been   * initialized for signing purposes.   */  protected static final int SIGN = 2;  /**   * Possible state value which signifies that this instance has been   * initialized for verification purposes.   */  protected static final int VERIFY = 3;  /** Current sate of this instance. */  protected int state = UNINITIALIZED;  private String algorithm;  Provider provider;  // Constructor.  // ------------------------------------------------------------------------  /**   * Constructs a new <code>Signature</code> instance for a designated digital   * signature algorithm.   *    * @param algorithm   *          the algorithm to use.   */  protected Signature(String algorithm)  {    this.algorithm = algorithm;    state = UNINITIALIZED;  }  /**   * Returns an instance of <code>Signature</code> representing the specified   * signature.   *    * @param algorithm   *          the algorithm to use.   * @return a new instance repesenting the desired algorithm.   * @throws NoSuchAlgorithmException   *           if the algorithm is not implemented by any provider.   */  public static Signature getInstance(String algorithm)    throws NoSuchAlgorithmException  {    Provider[] p = Security.getProviders();    for (int i = 0; i < p.length; i++)      {        try          {            return getInstance(algorithm, p[i]);          }	catch (NoSuchAlgorithmException e)	  {	    // Ignored.	  }      }    throw new NoSuchAlgorithmException(algorithm);  }  /**   * Returns an instance of <code>Signature</code> representing the specified   * signature from the named provider.   *    * @param algorithm   *          the algorithm to use.   * @param provider   *          the name of the provider to use.   * @return a new instance repesenting the desired algorithm.   * @throws IllegalArgumentException if <code>provider</code> is   *           <code>null</code> or is an empty string.   * @throws NoSuchProviderException   *           if the named provider was not found.   * @throws NoSuchAlgorithmException   *           if the algorithm is not implemented by the named provider.   */  public static Signature getInstance(String algorithm, String provider)    throws NoSuchAlgorithmException, NoSuchProviderException  {    if (provider == null || provider.length() == 0)      throw new IllegalArgumentException("Illegal provider");    Provider p = Security.getProvider(provider);    if (p == null)      throw new NoSuchProviderException(provider);    return getInstance(algorithm, p);  }  /**   * Returns an instance of <code>Signature</code> representing the specified   * signature from the specified {@link Provider}.   *    * @param algorithm   *          the algorithm to use.   * @param provider   *          the {@link Provider} to use.   * @return a new instance repesenting the desired algorithm.   * @throws NoSuchAlgorithmException   *           if the algorithm is not implemented by the {@link Provider}.   */  public static Signature getInstance(String algorithm, Provider provider)    throws NoSuchAlgorithmException  {    if (provider == null)      throw new IllegalArgumentException("Illegal provider");    Signature result = null;    Object o = null;    try      {        o = Engine.getInstance(SIGNATURE, algorithm, provider);      }    catch (java.lang.reflect.InvocationTargetException ite)      {        throw new NoSuchAlgorithmException(algorithm);      }    if (o instanceof SignatureSpi)      {        result = new DummySignature((SignatureSpi) o, algorithm);      }    else if (o instanceof Signature)      {        result = (Signature) o;        result.algorithm = algorithm;      }    else      {        throw new NoSuchAlgorithmException(algorithm);      }    result.provider = provider;    return result;  }  /**   * Returns the {@link Provider} of this instance.   *    * @return the {@link Provider} of this instance.   */  public final Provider getProvider()  {    return provider;  }  /**   * Initializes this instance with the public key for verification purposes.   *    * @param publicKey   *          the public key to verify with.   * @throws InvalidKeyException   *           if the key is invalid.   */  public final void initVerify(PublicKey publicKey) throws InvalidKeyException  {    state = VERIFY;    engineInitVerify(publicKey);  }  /**   * Verify a signature with a designated {@link Certificate}. This is a FIPS   * 140-1 compatible method since it verifies a signature with a certificate.   *    * <p>If the {@link Certificate} is an X.509 one, has a <i>KeyUsage</i>   * parameter and that parameter indicates this key is not to be used for   * signing then an exception is thrown.</p>   *    * @param certificate   *          a {@link Certificate} containing a public key to verify with.   * @throws InvalidKeyException if the key is invalid.   */  public final void initVerify(Certificate certificate)    throws InvalidKeyException  {    state = VERIFY;    if (certificate.getType().equals("X509"))      {        X509Certificate cert = (X509Certificate) certificate;        boolean[]array = cert.getKeyUsage();        if (array != null && array[0] == false)          throw new InvalidKeyException(              "KeyUsage of this Certificate indicates it cannot be used for digital signing");      }    this.initVerify(certificate.getPublicKey());  }  /**   * Initializes this class with the private key for signing purposes.   *    * @param privateKey

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?