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

File BcRSAKeyPairGenerator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
9
3
1
86
49
4
0.44
3
3
1.33

Classes

Class Line # Actions
BcRSAKeyPairGenerator 46 9 0% 4 4
0.7142857371.4%
 

Contributing tests

This file is covered by 4 tests. .

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.RSAKeyPairGenerator;
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.RSAKeyGenerationParameters;
36   
37    /**
38    * Generate new RSA key pair.
39    *
40    * @version $Id: 78a75f927b1afc4449034895459ef2f984266cb8 $
41    * @since 5.4M1
42    */
43    @Component
44    @Singleton
45    @Named("RSA")
 
46    public class BcRSAKeyPairGenerator extends AbstractBcKeyPairGenerator
47    {
48    @Inject
49    @Named("RSA")
50    private AsymmetricKeyFactory factory;
51   
52    @Inject
53    private Provider<SecureRandom> random;
54   
 
55  0 toggle @Override
56    protected AsymmetricKeyFactory getFactory()
57    {
58  0 return this.factory;
59    }
60   
 
61  2 toggle @Override
62    public AsymmetricKeyPair generate()
63    {
64  2 return generate(new RSAKeyGenerationParameters());
65    }
66   
 
67  6 toggle @Override
68    public AsymmetricKeyPair generate(KeyGenerationParameters parameters)
69    {
70  6 if (!(parameters instanceof RSAKeyGenerationParameters)) {
71  0 throw new IllegalArgumentException("Invalid parameters for RSA key generator: "
72    + parameters.getClass().getName());
73    }
74   
75  6 RSAKeyGenerationParameters params = (RSAKeyGenerationParameters) parameters;
76   
77  6 org.bouncycastle.crypto.params.RSAKeyGenerationParameters genParam =
78    new org.bouncycastle.crypto.params.RSAKeyGenerationParameters(
79    params.getPublicExponent(), this.random.get(), params.getStrength() * 8, params.getCertainty());
80   
81  6 AsymmetricCipherKeyPairGenerator generator = new RSAKeyPairGenerator();
82  6 generator.init(genParam);
83   
84  6 return getKeyPair(generator.generateKeyPair());
85    }
86    }