PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/extern/spongycastle/prov/src/main/java/org/spongycastle/jcajce/provider/asymmetric/dsa/KeyPairGeneratorSpi.java

https://gitlab.com/vizilo/fdroidclient
Java | 82 lines | 69 code | 13 blank | 0 comment | 10 complexity | 4707092588d354e78077d73765985bcc MD5 | raw file
 1package org.spongycastle.jcajce.provider.asymmetric.dsa;
 2
 3import java.security.InvalidAlgorithmParameterException;
 4import java.security.InvalidParameterException;
 5import java.security.KeyPair;
 6import java.security.SecureRandom;
 7import java.security.spec.AlgorithmParameterSpec;
 8import java.security.spec.DSAParameterSpec;
 9
10import org.spongycastle.crypto.AsymmetricCipherKeyPair;
11import org.spongycastle.crypto.generators.DSAKeyPairGenerator;
12import org.spongycastle.crypto.generators.DSAParametersGenerator;
13import org.spongycastle.crypto.params.DSAKeyGenerationParameters;
14import org.spongycastle.crypto.params.DSAParameters;
15import org.spongycastle.crypto.params.DSAPrivateKeyParameters;
16import org.spongycastle.crypto.params.DSAPublicKeyParameters;
17
18public class KeyPairGeneratorSpi
19    extends java.security.KeyPairGenerator
20{
21    DSAKeyGenerationParameters param;
22    DSAKeyPairGenerator engine = new DSAKeyPairGenerator();
23    int strength = 1024;
24    int certainty = 20;
25    SecureRandom random = new SecureRandom();
26    boolean initialised = false;
27
28    public KeyPairGeneratorSpi()
29    {
30        super("DSA");
31    }
32
33    public void initialize(
34        int strength,
35        SecureRandom random)
36    {
37        if (strength < 512 || strength > 4096 || ((strength < 1024) && strength % 64 != 0) || (strength >= 1024 && strength % 1024 != 0))
38        {
39            throw new InvalidParameterException("strength must be from 512 - 4096 and a multiple of 1024 above 1024");
40        }
41
42        this.strength = strength;
43        this.random = random;
44    }
45
46    public void initialize(
47        AlgorithmParameterSpec params,
48        SecureRandom random)
49        throws InvalidAlgorithmParameterException
50    {
51        if (!(params instanceof DSAParameterSpec))
52        {
53            throw new InvalidAlgorithmParameterException("parameter object not a DSAParameterSpec");
54        }
55        DSAParameterSpec dsaParams = (DSAParameterSpec)params;
56
57        param = new DSAKeyGenerationParameters(random, new DSAParameters(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG()));
58
59        engine.init(param);
60        initialised = true;
61    }
62
63    public KeyPair generateKeyPair()
64    {
65        if (!initialised)
66        {
67            DSAParametersGenerator pGen = new DSAParametersGenerator();
68
69            pGen.init(strength, certainty, random);
70            param = new DSAKeyGenerationParameters(random, pGen.generateParameters());
71            engine.init(param);
72            initialised = true;
73        }
74
75        AsymmetricCipherKeyPair pair = engine.generateKeyPair();
76        DSAPublicKeyParameters pub = (DSAPublicKeyParameters)pair.getPublic();
77        DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters)pair.getPrivate();
78
79        return new KeyPair(new BCDSAPublicKey(pub),
80            new BCDSAPrivateKey(priv));
81    }
82}