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

File DefaultKeyGenerator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

2
8
3
1
73
39
4
0.5
2.67
3
1.33

Classes

Class Line # Actions
DefaultKeyGenerator 42 8 0% 4 2
0.8461538684.6%
 

Contributing tests

This file is covered by 6 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.symmetric.generator;
21   
22    import java.security.SecureRandom;
23   
24    import javax.inject.Inject;
25    import javax.inject.Provider;
26    import javax.inject.Singleton;
27   
28    import org.bouncycastle.crypto.CipherKeyGenerator;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.crypto.KeyGenerator;
31    import org.xwiki.crypto.params.generator.KeyGenerationParameters;
32    import org.xwiki.crypto.params.generator.symmetric.GenericKeyGenerationParameters;
33   
34    /**
35    * Default key generator, not taking care of specific targeted algorithm.
36    *
37    * @version $Id: 080fabe0a755ad4df0036738444eeb4215229fb7 $
38    * @since 5.4M1
39    */
40    @Component
41    @Singleton
 
42    public class DefaultKeyGenerator implements KeyGenerator
43    {
44    @Inject
45    private Provider<SecureRandom> random;
46   
 
47  1 toggle @Override
48    public byte[] generate()
49    {
50  1 throw new UnsupportedOperationException("Knowing the key strength is required to generate a key.");
51    }
52   
 
53  13 toggle @Override
54    public byte[] generate(KeyGenerationParameters parameters)
55    {
56  13 if (!(parameters instanceof GenericKeyGenerationParameters)) {
57  0 throw new IllegalArgumentException("Invalid parameters for generic key generator: "
58    + parameters.getClass().getName());
59    }
60   
61  13 GenericKeyGenerationParameters params = (GenericKeyGenerationParameters) parameters;
62   
63  13 CipherKeyGenerator generator = getKeyGenerator();
64  13 generator.init(new org.bouncycastle.crypto.KeyGenerationParameters(this.random.get(),
65    params.getStrength() * 8));
66  13 return generator.generateKey();
67    }
68   
 
69  3 toggle protected CipherKeyGenerator getKeyGenerator()
70    {
71  3 return new CipherKeyGenerator();
72    }
73    }