1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.crypto.password.internal.pbe.factory

File AbstractBcPBCipherFactory.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart6.png
72% of files have more coverage

Code metrics

0
11
9
1
124
67
10
0.91
1.22
9
1.11

Classes

Class Line # Actions
AbstractBcPBCipherFactory 39 11 0% 10 9
0.5555%
 

Contributing tests

This file is covered by 13 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.password.internal.pbe.factory;
21   
22    import javax.inject.Inject;
23   
24    import org.bouncycastle.asn1.ASN1Encodable;
25    import org.bouncycastle.asn1.ASN1Sequence;
26    import org.xwiki.crypto.cipher.CipherFactory;
27    import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters;
28    import org.xwiki.crypto.password.KeyDerivationFunctionFactory;
29    import org.xwiki.crypto.password.PasswordBasedCipher;
30    import org.xwiki.crypto.password.PasswordBasedCipherFactory;
31    import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters;
32   
33    /**
34    * Abstract base class for password based cipher factory based on Bouncy Castle.
35    *
36    * @version $Id: 1b3c2c3603a383bad4813473214f60ecce14d73a $
37    * @since 5.4M1
38    */
 
39    public abstract class AbstractBcPBCipherFactory implements PasswordBasedCipherFactory
40    {
41    private static final RuntimeException UNSUPPORTED =
42    new UnsupportedOperationException("Sorry, this factory does implement any concrete cipher.");
43   
44    @Inject
45    private KeyDerivationFunctionFactory kdfFactory;
46   
47    /**
48    * @return an instance of the key derivation function factory in use for this password based cipher.
49    */
 
50  28 toggle protected KeyDerivationFunctionFactory getKDFFactory()
51    {
52  28 return this.kdfFactory;
53    }
54   
 
55  22 toggle private CipherFactory safeGetCipherFactory()
56    {
57  22 try {
58  22 return getCipherFactory();
59    } catch (UnsupportedOperationException e) {
60  0 throw UNSUPPORTED;
61    }
62    }
63   
 
64  0 toggle @Override
65    public String getCipherAlgorithmName()
66    {
67  0 return safeGetCipherFactory().getCipherAlgorithmName();
68    }
69   
 
70  3 toggle @Override
71    public int getIVSize()
72    {
73  3 return safeGetCipherFactory().getIVSize();
74    }
75   
 
76  9 toggle @Override
77    public int getKeySize()
78    {
79  9 return safeGetCipherFactory().getKeySize();
80    }
81   
 
82  0 toggle @Override
83    public int[] getSupportedKeySizes()
84    {
85  0 return safeGetCipherFactory().getSupportedKeySizes();
86    }
87   
 
88  10 toggle @Override
89    public boolean isSupportedKeySize(int keySize)
90    {
91  10 return safeGetCipherFactory().isSupportedKeySize(keySize);
92    }
93   
 
94  0 toggle @Override
95    public PasswordBasedCipher getInstance(boolean forEncryption, SymmetricCipherParameters password,
96    KeyDerivationFunctionParameters parameters)
97    {
98  0 throw new UnsupportedOperationException("Sorry, no concrete implementation to create an instance.");
99    }
100   
 
101  0 toggle @Override
102    public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, byte[] encoded)
103    {
104  0 return getInstance(forEncryption, password, ASN1Sequence.getInstance(encoded));
105    }
106   
107    /**
108    * @return an instance of the block cipher factory in use for this password based cipher.
109    */
110    protected abstract CipherFactory getCipherFactory();
111   
112    /**
113    * Get a Password based cipher that is Bouncy Castle based.
114    *
115    * The ASN.1 parameter will be parsed as an algorithm identifier and should contains the derivation function and
116    * cipher parameters appropriately.
117    *
118    * @param forEncryption if true the cipher is initialised for encryption, if false for decryption.
119    * @param password the password that will be used to derive the key.
120    * @param parameters ASN.1 representation of the parameters needed to define a Password Based Cipher.
121    * @return a initialized key derivation function with a specific password bytes conversion mode.
122    */
123    public abstract PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, ASN1Encodable parameters);
124    }