1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.asymmetric.generator

File BcDHKeyPairGenerator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
13
4
1
101
63
7
0.54
3.25
4
1.75

Classes

Class Line # Actions
BcDHKeyPairGenerator 48 13 0% 7 23
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.internal.asymmetric.generator;
21   
22    import java.security.SecureRandom;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
30    import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.crypto.AsymmetricKeyFactory;
33    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricKeyPair;
34    import org.xwiki.crypto.params.generator.KeyGenerationParameters;
35    import org.xwiki.crypto.params.generator.asymmetric.DHKeyGenerationParameters;
36    import org.xwiki.crypto.params.generator.asymmetric.DHKeyParametersGenerationParameters;
37    import org.xwiki.crypto.params.generator.asymmetric.DHKeyValidationParameters;
38   
39    /**
40    * Generate new Diffie-Hellman key pair.
41    *
42    * @version $Id: 62f9d958c5530822ef09dae8118c1c05076624fe $
43    * @since 5.4M1
44    */
45    @Component
46    @Singleton
47    @Named("DH")
 
48    public class BcDHKeyPairGenerator extends AbstractBcKeyPairGenerator
49    {
50    @Inject
51    @Named("DH")
52    private AsymmetricKeyFactory factory;
53   
54    @Inject
55    private Provider<SecureRandom> random;
56   
 
57  0 toggle @Override
58    protected AsymmetricKeyFactory getFactory()
59    {
60  0 return this.factory;
61    }
62   
 
63  0 toggle @Override
64    public AsymmetricKeyPair generate()
65    {
66  0 return generate(new DHKeyParametersGenerationParameters());
67    }
68   
 
69  0 toggle @Override
70    public AsymmetricKeyPair generate(KeyGenerationParameters parameters)
71    {
72  0 org.bouncycastle.crypto.params.DHParameters keyGenParams;
73   
74  0 if (parameters instanceof DHKeyParametersGenerationParameters) {
75  0 keyGenParams = BcDHKeyParameterGenerator.getDhParameters(this.random.get(),
76    (DHKeyParametersGenerationParameters) parameters);
77  0 } else if (parameters instanceof DHKeyGenerationParameters) {
78  0 keyGenParams = getDhParameters((DHKeyGenerationParameters) parameters);
79    } else {
80  0 throw new IllegalArgumentException("Invalid parameters for DSA key generator: "
81    + parameters.getClass().getName());
82    }
83   
84  0 AsymmetricCipherKeyPairGenerator generator = new DHKeyPairGenerator();
85  0 generator.init(new org.bouncycastle.crypto.params.DHKeyGenerationParameters(this.random.get(),
86    keyGenParams));
87   
88  0 return getKeyPair(generator.generateKeyPair());
89    }
90   
 
91  0 toggle private org.bouncycastle.crypto.params.DHParameters getDhParameters(DHKeyGenerationParameters parameters)
92    {
93  0 DHKeyValidationParameters dhValidParams = parameters.getValidationParameters();
94   
95  0 return new org.bouncycastle.crypto.params.DHParameters(parameters.getP(), parameters.getG(), parameters.getQ(),
96    parameters.getM() * 8, parameters.getL() * 8, parameters.getJ(),
97  0 (dhValidParams != null) ? new org.bouncycastle.crypto.params.DHValidationParameters(
98    dhValidParams.getSeed(),
99    dhValidParams.getCounter()) : null);
100    }
101    }