dsskeypairgenerator.java

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

JAVA
446
字号
/* DSSKeyPairGenerator.java --    Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.This file is a 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 of the License, or (atyour 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; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking 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 gnu.java.security.key.dss;import gnu.java.security.Registry;import gnu.java.security.hash.Sha160;import gnu.java.security.key.IKeyPairGenerator;import gnu.java.security.util.PRNG;import java.io.PrintWriter;import java.math.BigInteger;import java.security.KeyPair;import java.security.PrivateKey;import java.security.PublicKey;import java.security.SecureRandom;import java.security.spec.DSAParameterSpec;import java.util.Map;/** * <p>A key-pair generator for asymetric keys to use in conjunction with the DSS * (Digital Signature Standard).</p> * * References:<br> * <a href="http://www.itl.nist.gov/fipspubs/fip186.htm">Digital Signature * Standard (DSS)</a>, Federal Information Processing Standards Publication 186. * National Institute of Standards and Technology. */public class DSSKeyPairGenerator implements IKeyPairGenerator{  // Debugging methods and variables  // -------------------------------------------------------------------------  private static final String NAME = "dss";  private static final boolean DEBUG = false;  private static final int debuglevel = 5;  private static final PrintWriter err = new PrintWriter(System.out, true);  private static void debug(String s)  {    err.println(">>> " + NAME + ": " + s);  }  // Constants and variables  // -------------------------------------------------------------------------  /** The BigInteger constant 2. */  private static final BigInteger TWO = new BigInteger("2");  /** Property name of the length (Integer) of the modulus (p) of a DSS key. */  public static final String MODULUS_LENGTH = "gnu.crypto.dss.L";  /**   * Property name of the Boolean indicating wether or not to use default pre-   * computed values of <code>p</code>, <code>q</code> and <code>g</code> for   * a given modulus length. The ultimate behaviour of this generator with   * regard to using pre-computed parameter sets will depend on the value of   * this property and of the following one {@link #STRICT_DEFAULTS}:   *    * <ol>   *   <li>If this property is {@link Boolean#FALSE} then this generator   *   will accept being setup for generating parameters for any modulus length   *   provided the modulus length is between <code>512</code> and   *   <code>1024</code>, and is of the form <code>512 + 64 * n</code>. In   *   addition, a new paramter set will always be generated; i.e. no pre-   *   computed values are used.</li>   *      *   <li>If this property is {@link Boolean#TRUE} and the value of   *   {@link #STRICT_DEFAULTS} is also {@link Boolean#TRUE} then this generator   *   will only accept being setup for generating parameters for modulus   *   lengths of <code>512</code>, <code>768</code> and <code>1024</code>. Any   *   other value, of the modulus length, even if between <code>512</code> and   *   <code>1024</code>, and of the form <code>512 + 64 * n</code>, will cause   *   an {@link IllegalArgumentException} to be thrown. When those modulus   *   length (<code>512</code>, <code>768</code>, and <code>1024</code>) are   *   specified, the paramter set is always the same.</li>   *      *   <li>Finally, if this property is {@link Boolean#TRUE} and the value of   *   {@link #STRICT_DEFAULTS} is {@link Boolean#FALSE} then this generator   *   will behave as in point 1 above, except that it will use pre-computed   *   values when possible; i.e. the modulus length is one of <code>512</code>,   *   <code>768</code>, or <code>1024</code>.</li>   * </ol>   *    * The default value of this property is {@link Boolean#TRUE}.   */  public static final String USE_DEFAULTS = "gnu.crypto.dss.use.defaults";  /**   * Property name of the Boolean indicating wether or not to generate new   * parameters, even if the modulus length <i>L</i> is not one of the pre-   * computed defaults (value {@link Boolean#FALSE}), or throw an exception   * (value {@link Boolean#TRUE}) -- the exception in this case is an   * {@link IllegalArgumentException}. The default value for this property is   * {@link Boolean#FALSE}. The ultimate behaviour of this generator will   * depend on the values of this and {@link #USE_DEFAULTS} properties -- see   * {@link #USE_DEFAULTS} for more information.   */  public static final String STRICT_DEFAULTS = "gnu.crypto.dss.strict.defaults";  /**   * Property name of an optional {@link SecureRandom} instance to use. The   * default is to use a classloader singleton from {@link PRNG}.   */  public static final String SOURCE_OF_RANDOMNESS = "gnu.crypto.dss.prng";  /**   * Property name of an optional {@link DSAParameterSpec} instance to use for   * this generator's <code>p</code>, <code>q</code>, and <code>g</code> values.   * The default is to generate these values or use pre-computed ones,   * depending on the value of the <code>USE_DEFAULTS</code> attribute.   */  public static final String DSS_PARAMETERS = "gnu.crypto.dss.params";  /**   * Property name of the preferred encoding format to use when externalizing   * generated instance of key-pairs from this generator. The property is taken   * to be an {@link Integer} that encapsulates an encoding format identifier.   */  public static final String PREFERRED_ENCODING_FORMAT = "gnu.crypto.dss.encoding";  /** Default value for the modulus length. */  public static final int DEFAULT_MODULUS_LENGTH = 1024;  /** Default encoding format to use when none was specified. */  private static final int DEFAULT_ENCODING_FORMAT = Registry.RAW_ENCODING_ID;  /** Initial SHS context. */  private static final int[] T_SHS = new int[] { 0x67452301, 0xEFCDAB89,                                                0x98BADCFE, 0x10325476,                                                0xC3D2E1F0 };  // from jdk1.3.1/docs/guide/security/CryptoSpec.html#AppB  public static final DSAParameterSpec KEY_PARAMS_512 = new DSAParameterSpec(                                                                             new BigInteger(                                                                                            "fca682ce8e12caba26efccf7110e526db078b05edecbcd1eb4a208f3ae1617ae"                                                                                                + "01f35b91a47e6df63413c5e12ed0899bcd132acd50d99151bdc43ee737592e17",                                                                                            16),                                                                             new BigInteger(                                                                                            "962eddcc369cba8ebb260ee6b6a126d9346e38c5",                                                                                            16),                                                                             new BigInteger(                                                                                            "678471b27a9cf44ee91a49c5147db1a9aaf244f05a434d6486931d2d14271b9e"                                                                                                + "35030b71fd73da179069b32e2935630e1c2062354d0da20a6c416e50be794ca4",                                                                                            16));  public static final DSAParameterSpec KEY_PARAMS_768 = new DSAParameterSpec(                                                                             new BigInteger(                                                                                            "e9e642599d355f37c97ffd3567120b8e25c9cd43e927b3a9670fbec5d8901419"                                                                                                + "22d2c3b3ad2480093799869d1e846aab49fab0ad26d2ce6a22219d470bce7d77"                                                                                                + "7d4a21fbe9c270b57f607002f3cef8393694cf45ee3688c11a8c56ab127a3daf",                                                                                            16),                                                                             new BigInteger(                                                                                            "9cdbd84c9f1ac2f38d0f80f42ab952e7338bf511",                                                                                            16),                                                                             new BigInteger(                                                                                            "30470ad5a005fb14ce2d9dcd87e38bc7d1b1c5facbaecbe95f190aa7a31d23c4"                                                                                                + "dbbcbe06174544401a5b2c020965d8c2bd2171d3668445771f74ba084d2029d8"                                                                                                + "3c1c158547f3a9f1a2715be23d51ae4d3e5a1f6a7064f316933a346d3f529252",                                                                                            16));  public static final DSAParameterSpec KEY_PARAMS_1024 = new DSAParameterSpec(                                                                              new BigInteger(                                                                                             "fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669"                                                                                                 + "455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7"                                                                                                 + "6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb"                                                                                                 + "83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7",                                                                                             16),                                                                              new BigInteger(                                                                                             "9760508f15230bccb292b982a2eb840bf0581cf5",                                                                                             16),                                                                              new BigInteger(                                                                                             "f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267"                                                                                                 + "5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1"                                                                                                 + "3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b"                                                                                                 + "cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a",                                                                                             16));  private static final BigInteger TWO_POW_160 = TWO.pow(160);  /** The length of the modulus of DSS keys generated by this instance. */  private int L;  /** The optional {@link SecureRandom} instance to use. */

⌨️ 快捷键说明

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