1 7 8 package java.security; 9 10 import java.io.*; 11 import java.security.spec.AlgorithmParameterSpec ; 12 import java.security.spec.InvalidParameterSpecException ; 13 14 60 61 public class AlgorithmParameters { 62 63 private Provider provider; 65 66 private AlgorithmParametersSpi paramSpi; 68 69 private String algorithm; 71 72 private boolean initialized = false; 74 75 82 protected AlgorithmParameters(AlgorithmParametersSpi paramSpi, 83 Provider provider, String algorithm) 84 { 85 this.paramSpi = paramSpi; 86 this.provider = provider; 87 this.algorithm = algorithm; 88 } 89 90 95 public final String getAlgorithm() { 96 return this.algorithm; 97 } 98 99 119 public static AlgorithmParameters getInstance(String algorithm) 120 throws NoSuchAlgorithmException { 121 try { 122 Object [] objs = Security.getImpl(algorithm, "AlgorithmParameters", 123 (String )null); 124 return new AlgorithmParameters ((AlgorithmParametersSpi )objs[0], 125 (Provider )objs[1], 126 algorithm); 127 } catch(NoSuchProviderException e) { 128 throw new NoSuchAlgorithmException (algorithm + " not found"); 129 } 130 } 131 132 159 public static AlgorithmParameters getInstance(String algorithm, 160 String provider) 161 throws NoSuchAlgorithmException , NoSuchProviderException 162 { 163 if (provider == null || provider.length() == 0) 164 throw new IllegalArgumentException ("missing provider"); 165 Object [] objs = Security.getImpl(algorithm, "AlgorithmParameters", 166 provider); 167 return new AlgorithmParameters ((AlgorithmParametersSpi )objs[0], 168 (Provider )objs[1], 169 algorithm); 170 } 171 172 198 public static AlgorithmParameters getInstance(String algorithm, 199 Provider provider) 200 throws NoSuchAlgorithmException 201 { 202 if (provider == null) 203 throw new IllegalArgumentException ("missing provider"); 204 Object [] objs = Security.getImpl(algorithm, "AlgorithmParameters", 205 provider); 206 return new AlgorithmParameters ((AlgorithmParametersSpi )objs[0], 207 (Provider )objs[1], 208 algorithm); 209 } 210 211 216 public final Provider getProvider() { 217 return this.provider; 218 } 219 220 230 public final void init(AlgorithmParameterSpec paramSpec) 231 throws InvalidParameterSpecException 232 { 233 if (this.initialized) 234 throw new InvalidParameterSpecException ("already initialized"); 235 paramSpi.engineInit(paramSpec); 236 this.initialized = true; 237 } 238 239 250 public final void init(byte[] params) throws IOException { 251 if (this.initialized) 252 throw new IOException("already initialized"); 253 paramSpi.engineInit(params); 254 this.initialized = true; 255 } 256 257 272 public final void init(byte[] params, String format) throws IOException { 273 if (this.initialized) 274 throw new IOException("already initialized"); 275 paramSpi.engineInit(params, format); 276 this.initialized = true; 277 } 278 279 296 public final <T extends AlgorithmParameterSpec > 297 T getParameterSpec(Class <T> paramSpec) 298 throws InvalidParameterSpecException 299 { 300 if (this.initialized == false) { 301 throw new InvalidParameterSpecException ("not initialized"); 302 } 303 return paramSpi.engineGetParameterSpec(paramSpec); 304 } 305 306 316 public final byte[] getEncoded() throws IOException 317 { 318 if (this.initialized == false) { 319 throw new IOException("not initialized"); 320 } 321 return paramSpi.engineGetEncoded(); 322 } 323 324 338 public final byte[] getEncoded(String format) throws IOException 339 { 340 if (this.initialized == false) { 341 throw new IOException("not initialized"); 342 } 343 return paramSpi.engineGetEncoded(format); 344 } 345 346 352 public final String toString() { 353 if (this.initialized == false) { 354 return null; 355 } 356 return paramSpi.engineToString(); 357 } 358 } 359 | Popular Tags |