1 /* $Id: KeyGenerator.java,v 1.4 2000/01/15 03:25:04 gelderen Exp $
2  *
3  * Copyright (C) 1995-1999 The Cryptix Foundation Limited.
4  * All rights reserved.
5  *
6  * Use, modification, copying and distribution of this software is subject
7  * the terms and conditions of the Cryptix General Licence. You should have
8  * received a copy of the Cryptix General Licence along with this library;
9  * if not, you can download a copy from http://www.cryptix.org/ .
10  */
11 package javax.crypto;
12 
13 
14 import java.security.InvalidAlgorithmParameterException;
15 import java.security.NoSuchAlgorithmException;
16 import java.security.NoSuchProviderException;
17 import java.security.Provider;
18 import java.security.SecureRandom;
19 import java.security.spec.AlgorithmParameterSpec;
20 
21 
22 public class KeyGenerator
23 {
24     private final KeyGeneratorSpi spi;
25     private final Provider        provider;
26     private final String          algorithm;
27 
28 
KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)29     protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider,
30                            String algorithm)
31     {
32         this.spi       = keyGenSpi;
33         this.provider  = provider;
34         this.algorithm = algorithm;
35     }
36 
37 
getAlgorithm()38     public final String getAlgorithm() {
39         return this.algorithm;
40     }
41 
42 
43     public static final KeyGenerator
getInstance(String algorithm)44     getInstance(String algorithm)
45     throws NoSuchAlgorithmException
46     {
47         Object[] o =
48             Support.getImplementation("KeyGenerator", algorithm);
49         return
50             new KeyGenerator((KeyGeneratorSpi)o[0], (Provider)o[1], algorithm);
51     }
52 
53 
54     public static final KeyGenerator
getInstance(String algorithm, String provider)55     getInstance(String algorithm, String provider)
56     throws NoSuchAlgorithmException, NoSuchProviderException
57     {
58         Object[] o =
59             Support.getImplementation("KeyGenerator", algorithm, provider);
60         return
61             new KeyGenerator((KeyGeneratorSpi)o[0], (Provider)o[1], algorithm);
62     }
63 
64 
getProvider()65     public final Provider getProvider() {
66         return this.provider;
67     }
68 
69 
init(SecureRandom random)70     public final void init(SecureRandom random) {
71         this.spi.engineInit(random);
72     }
73 
74 
init(AlgorithmParameterSpec params)75     public final void init(AlgorithmParameterSpec params)
76     throws InvalidAlgorithmParameterException {
77         this.spi.engineInit(params, new SecureRandom());
78     }
79 
80 
init(AlgorithmParameterSpec params, SecureRandom random)81     public final void init(AlgorithmParameterSpec params, SecureRandom random)
82     throws InvalidAlgorithmParameterException {
83         this.spi.engineInit(params, random);
84     }
85 
86 
init(int strength)87     public final void init(int strength) {
88         this.spi.engineInit(strength, new SecureRandom());
89     }
90 
91 
init(int strength, SecureRandom random)92     public final void init(int strength, SecureRandom random) {
93         this.spi.engineInit(strength, random);
94     }
95 
96 
generateKey()97     public final SecretKey generateKey() {
98         return this.spi.engineGenerateKey();
99     }
100 }