Overview Package Class Use Source Tree Index Deprecated About
GNU Classpath (0.95)
Frames | No Frames

Source for java.security.AlgorithmParameterGenerator

 1:  /* AlgorithmParameterGenerator.java --- Algorithm Parameter Generator
 2:  Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
 3: 
 4: This file is part of GNU Classpath.
 5: 
 6: GNU Classpath is free software; you can redistribute it and/or modify
 7: it under the terms of the GNU General Public License as published by
 8: the Free Software Foundation; either version 2, or (at your option)
 9: any later version.
 10: 
 11: GNU Classpath is distributed in the hope that it will be useful, but
 12: WITHOUT ANY WARRANTY; without even the implied warranty of
 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14: General Public License for more details.
 15: 
 16: You should have received a copy of the GNU General Public License
 17: along with GNU Classpath; see the file COPYING. If not, write to the
 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 19: 02110-1301 USA.
 20: 
 21: Linking this library statically or dynamically with other modules is
 22: making a combined work based on this library. Thus, the terms and
 23: conditions of the GNU General Public License cover the whole
 24: combination.
 25: 
 26: As a special exception, the copyright holders of this library give you
 27: permission to link this library with independent modules to produce an
 28: executable, regardless of the license terms of these independent
 29: modules, and to copy and distribute the resulting executable under
 30: terms of your choice, provided that you also meet, for each linked
 31: independent module, the terms and conditions of the license of that
 32: module. An independent module is a module which is not derived from
 33: or based on this library. If you modify this library, you may extend
 34: this exception to your version of the library, but you are not
 35: obligated to do so. If you do not wish to do so, delete this
 36: exception statement from your version. */
 37: 
 38: 
 39:  package java.security;
 40: 
 41:  import gnu.java.security.Engine;
 42: 
 43:  import java.lang.reflect.InvocationTargetException;
 44:  import java.security.spec.AlgorithmParameterSpec;
 45: 
 46:  /**
 47:  * <code>AlgorithmParameterGenerator</code> is used to generate algorithm
 48:  * parameters for specified algorithms.
 49:  * 
 50:  * <p>In case the client does not explicitly initialize the
 51:  * <code>AlgorithmParameterGenerator</code> (via a call to an
 52:  * <code>init()</code> method), each provider must supply (and document) a
 53:  * default initialization. For example, the <b>GNU</b> provider uses a default
 54:  * modulus prime size of <code>1024</code> bits for the generation of <i>DSA</i>
 55:  * parameters.
 56:  *
 57:  * @author Mark Benvenuto
 58:  * @since 1.2
 59:  * @see AlgorithmParameters
 60:  * @see AlgorithmParameterSpec
 61:  */
 62:  public class AlgorithmParameterGenerator
 63: {
 64:  /** Service name for algorithm parameter generators. */
 65:  private static final String ALGORITHM_PARAMETER_GENERATOR =
 66:  "AlgorithmParameterGenerator";
 67: 
 68:  private AlgorithmParameterGeneratorSpi paramGenSpi;
 69:  private Provider provider;
 70:  private String algorithm;
 71: 
 72:  /**
 73:  * Constructs a new instance of <code>AlgorithmParameterGenerator</code>.
 74:  * 
 75:  * @param paramGenSpi
 76:  * the generator to use.
 77:  * @param provider
 78:  * the provider to use.
 79:  * @param algorithm
 80:  * the algorithm to use.
 81:  */
 82:  protected AlgorithmParameterGenerator(AlgorithmParameterGeneratorSpi
 83:  paramGenSpi, Provider provider,
 84:  String algorithm)
 85:  {
 86:  this.paramGenSpi = paramGenSpi;
 87:  this.provider = provider;
 88:  this.algorithm = algorithm;
 89:  }
 90: 
 91:  /** @return the name of the algorithm. */
 92:  public final String getAlgorithm()
 93:  {
 94:  return algorithm;
 95:  }
 96: 
 97:  /**
 98:  * Returns a new <code>AlgorithmParameterGenerator</code> instance which
 99:  * generates algorithm parameters for the specified algorithm.
 100:  * 
 101:  * @param algorithm the name of algorithm to use.
 102:  * @return the new instance.
 103:  * @throws NoSuchAlgorithmException if <code>algorithm</code> is not
 104:  * implemented by any provider.
 105:  * @throws IllegalArgumentException if <code>algorithm</code> is
 106:  * <code>null</code> or is an empty string.
 107:  */
 108:  public static AlgorithmParameterGenerator getInstance(String algorithm)
 109:  throws NoSuchAlgorithmException
 110:  {
 111:  Provider[] p = Security.getProviders();
 112:  NoSuchAlgorithmException lastException = null;
 113:  for (int i = 0; i < p.length; i++)
 114:  try
 115:  {
 116:  return getInstance(algorithm, p[i]);
 117:  }
 118:  catch (NoSuchAlgorithmException x)
 119:  {
 120:  lastException = x;
 121:  }
 122:  if (lastException != null)
 123:  throw lastException;
 124:  throw new NoSuchAlgorithmException(algorithm);
 125:  }
 126: 
 127:  /**
 128:  * Returns a new <code>AlgorithmParameterGenerator</code> instance which
 129:  * generates algorithm parameters for the specified algorithm.
 130:  * 
 131:  * @param algorithm the name of algorithm to use.
 132:  * @param provider the name of the {@link Provider} to use.
 133:  * @return the new instance.
 134:  * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
 135:  * named provider.
 136:  * @throws NoSuchProviderException if the named provider was not found.
 137:  * @throws IllegalArgumentException if either <code>algorithm</code> or
 138:  * <code>provider</code> is <code>null</code> or empty.
 139:  */
 140:  public static AlgorithmParameterGenerator getInstance(String algorithm,
 141:  String provider)
 142:  throws NoSuchAlgorithmException, NoSuchProviderException
 143:  {
 144:  if (provider == null)
 145:  throw new IllegalArgumentException("provider MUST NOT be null");
 146:  provider = provider.trim();
 147:  if (provider.length() == 0)
 148:  throw new IllegalArgumentException("provider MUST NOT be empty");
 149:  Provider p = Security.getProvider(provider);
 150:  if (p == null)
 151:  throw new NoSuchProviderException(provider);
 152:  return getInstance(algorithm, p);
 153:  }
 154: 
 155:  /**
 156:  * Returns a new <code>AlgorithmParameterGenerator</code> instance which
 157:  * generates algorithm parameters for the specified algorithm.
 158:  * 
 159:  * @param algorithm the name of algorithm to use.
 160:  * @param provider the {@link Provider} to use.
 161:  * @return the new instance.
 162:  * @throws NoSuchAlgorithmException if the algorithm is not implemented by
 163:  * {@link Provider}.
 164:  * @throws IllegalArgumentException if either <code>algorithm</code> or
 165:  * <code>provider</code> is <code>null</code>, or if
 166:  * <code>algorithm</code> is an empty string.
 167:  * @since 1.4
 168:  * @see Provider
 169:  */
 170:  public static AlgorithmParameterGenerator getInstance(String algorithm,
 171:  Provider provider)
 172:  throws NoSuchAlgorithmException
 173:  {
 174:  StringBuilder sb = new StringBuilder()
 175:  .append("AlgorithmParameterGenerator for algorithm [")
 176:  .append(algorithm).append("] from provider[")
 177:  .append(provider).append("] could not be created");
 178:  Throwable cause;
 179:  try
 180:  {
 181:  Object spi = Engine.getInstance(ALGORITHM_PARAMETER_GENERATOR,
 182:  algorithm,
 183:  provider);
 184:  return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) spi,
 185:  provider,
 186:  algorithm);
 187:  }
 188:  catch (InvocationTargetException x)
 189:  {
 190:  cause = x.getCause();
 191:  if (cause instanceof NoSuchAlgorithmException)
 192:  throw (NoSuchAlgorithmException) cause;
 193:  if (cause == null)
 194:  cause = x;
 195:  }
 196:  catch (ClassCastException x)
 197:  {
 198:  cause = x;
 199:  }
 200:  NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
 201:  x.initCause(cause);
 202:  throw x;
 203:  }
 204: 
 205:  /** @return the {@link Provider} of this generator. */
 206:  public final Provider getProvider()
 207:  {
 208:  return provider;
 209:  }
 210: 
 211:  /**
 212:  * Initializes this instance with the specified size. Since no source of
 213:  * randomness is supplied, a default one will be used.
 214:  * 
 215:  * @param size
 216:  * size (in bits) to use.
 217:  */
 218:  public final void init(int size)
 219:  {
 220:  init(size, new SecureRandom());
 221:  }
 222: 
 223:  /**
 224:  * Initializes this instance with the specified key-size and source of
 225:  * randomness.
 226:  * 
 227:  * @param size
 228:  * the size (in bits) to use.
 229:  * @param random
 230:  * the {@link SecureRandom} to use.
 231:  */
 232:  public final void init(int size, SecureRandom random)
 233:  {
 234:  paramGenSpi.engineInit(size, random);
 235:  }
 236: 
 237:  /**
 238:  * Initializes this instance with the specified {@link AlgorithmParameterSpec}.
 239:  * Since no source of randomness is supplied, a default one will be used.
 240:  * 
 241:  * @param genParamSpec
 242:  * the {@link AlgorithmParameterSpec} to use.
 243:  * @throws InvalidAlgorithmParameterException
 244:  * if <code>genParamSpec</code> is invalid.
 245:  */
 246:  public final void init(AlgorithmParameterSpec genParamSpec)
 247:  throws InvalidAlgorithmParameterException
 248:  {
 249:  init(genParamSpec, new SecureRandom());
 250:  }
 251: 
 252:  /**
 253:  * Initializes this instance with the specified {@link AlgorithmParameterSpec}
 254:  * and source of randomness.
 255:  * 
 256:  * @param genParamSpec
 257:  * the {@link AlgorithmParameterSpec} to use.
 258:  * @param random
 259:  * the {@link SecureRandom} to use.
 260:  * @throws InvalidAlgorithmParameterException
 261:  * if <code>genParamSpec</code> is invalid.
 262:  */
 263:  public final void init(AlgorithmParameterSpec genParamSpec,
 264:  SecureRandom random)
 265:  throws InvalidAlgorithmParameterException
 266:  {
 267:  paramGenSpi.engineInit(genParamSpec, random);
 268:  }
 269: 
 270:  /** @return a new instance of {@link AlgorithmParameters}. */
 271:  public final AlgorithmParameters generateParameters()
 272:  {
 273:  return paramGenSpi.engineGenerateParameters();
 274:  }
 275: }
Overview Package Class Use Source Tree Index Deprecated About
GNU Classpath (0.95)

AltStyle によって変換されたページ (->オリジナル) /