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

File AbstractBcSymmetricCipherFactory.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
21
6
1
111
61
11
0.52
3.5
6
1.83

Classes

Class Line # Actions
AbstractBcSymmetricCipherFactory 36 21 0% 11 2
0.9428571594.3%
 

Contributing tests

This file is covered by 24 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.cipher.internal.symmetric.factory;
21   
22    import javax.inject.Named;
23   
24    import org.bouncycastle.crypto.BlockCipher;
25    import org.xwiki.component.annotation.Component;
26    import org.xwiki.crypto.cipher.CipherFactory;
27    import org.xwiki.crypto.params.cipher.symmetric.KeyParameter;
28    import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters;
29   
30    /**
31    * Abstract base class for a Symmetric Cipher Factory of Bouncy Castle cipher.
32    *
33    * @version $Id: 11f00675b5959dfc79cb84b7e7035ee73bad8a53 $
34    * @since 5.4M1
35    */
 
36    public abstract class AbstractBcSymmetricCipherFactory implements CipherFactory
37    {
38    /**
39    * @return a new cipher engine instance.
40    */
41    protected abstract BlockCipher getEngineInstance();
42   
43    /**
44    * @return a new initialized and wrapped cipher engine, implementing block chaining (CBC).
45    */
46    protected abstract BlockCipher getCipherInstance(boolean forEncryption, SymmetricCipherParameters parameters);
47   
48    /**
49    * Helper function to create supported key size arrays.
50    *
51    * @param minSize minimum size supported.
52    * @param maxSize maximum size supported.
53    * @param step intermediate step supported.
54    * @return an array of sizes.
55    */
 
56  10 toggle protected static int[] newKeySizeArray(int minSize, int maxSize, int step)
57    {
58  10 int[] result = new int[((maxSize - minSize) / step) + 1];
59  271 for (int i = minSize, j = 0; i <= maxSize; i += step, j++) {
60  261 result[j] = i;
61    }
62  10 return result;
63    }
64   
 
65  7 toggle @Override
66    public String getCipherAlgorithmName()
67    {
68  7 String hint = null;
69  7 Named named = this.getClass().getAnnotation(Named.class);
70  7 if (named != null) {
71  1 hint = named.value();
72    } else {
73  6 Component component = this.getClass().getAnnotation(Component.class);
74  6 if (component != null && component.hints().length > 0) {
75  6 hint = component.hints()[0];
76    }
77    }
78   
79  7 return hint;
80    }
81   
 
82  9 toggle @Override
83    public int getIVSize()
84    {
85  9 return getEngineInstance().getBlockSize();
86    }
87   
 
88  12 toggle @Override
89    public int getKeySize()
90    {
91  12 int[] sizes = getSupportedKeySizes();
92  12 return sizes[sizes.length - 1];
93    }
94   
 
95  10 toggle @Override
96    public boolean isSupportedKeySize(int keySize)
97    {
98  10 int[] sizes = getSupportedKeySizes();
99  10 for (int i : sizes) {
100  73 if (i == keySize) {
101  10 return true;
102    }
103    }
104  0 return false;
105    }
106   
 
107  28 toggle protected org.bouncycastle.crypto.CipherParameters getBcKeyParameter(KeyParameter parameter)
108    {
109  28 return new org.bouncycastle.crypto.params.KeyParameter(parameter.getKey());
110    }
111    }