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

File DefaultKeyDerivationFunctionFactory.java

 

Coverage histogram

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

Code metrics

4
15
5
1
93
56
8
0.53
3
5
1.6

Classes

Class Line # Actions
DefaultKeyDerivationFunctionFactory 47 15 0% 8 5
0.791666779.2%
 

Contributing tests

This file is covered by 15 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.kdf.factory;
21   
22    import javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.bouncycastle.asn1.ASN1Encodable;
26    import org.bouncycastle.asn1.ASN1Sequence;
27    import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.component.manager.ComponentLookupException;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.crypto.password.KeyDerivationFunction;
32    import org.xwiki.crypto.password.KeyDerivationFunctionFactory;
33    import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters;
34   
35    /**
36    * Default key derivation factory to create derivation function from encoded ASN.1 identifiers and parameters.
37    *
38    * This factory could only create key derivation function from previously serialized function, that use the ASN.1
39    * encoding standard. It delegate the creation operation to the appropriate factory. The factory is requested
40    * to the component manager using the algorithm OID as hint.
41    *
42    * @version $Id: 424b9af2d3b64010def60390345eee0c5396ba2b $
43    * @since 5.4M1
44    */
45    @Component
46    @Singleton
 
47    public class DefaultKeyDerivationFunctionFactory extends AbstractBcKDFFactory
48    {
49    @Inject
50    private ComponentManager manager;
51   
 
52  15 toggle @Override
53    public KeyDerivationFunction getInstance(KeyDerivationFunctionParameters params)
54    {
55  15 return getFactory(params.getAlgorithmName()).getInstance(params);
56    }
57   
 
58  2 toggle @Override
59    public KeyDerivationFunction getInstance(byte[] encoded)
60    {
61  2 KeyDerivationFunc func = KeyDerivationFunc.getInstance(ASN1Sequence.getInstance(encoded));
62  2 KeyDerivationFunctionFactory factory = getFactory(func.getAlgorithm().getId());
63  2 KeyDerivationFunction kdf = getBcInstance(factory, func);
64  2 if (kdf == null) {
65  0 kdf = factory.getInstance(encoded);
66    }
67  2 return kdf;
68    }
69   
 
70  13 toggle @Override
71    public KeyDerivationFunction getInstance(ASN1Encodable parameters)
72    {
73  13 KeyDerivationFunc func = KeyDerivationFunc.getInstance(parameters);
74  13 return getBcInstance(getFactory(func.getAlgorithm().getId()), func);
75    }
76   
 
77  15 toggle private KeyDerivationFunction getBcInstance(KeyDerivationFunctionFactory factory, KeyDerivationFunc func)
78    {
79  15 if (factory instanceof AbstractBcKDFFactory) {
80  15 return ((AbstractBcKDFFactory) factory).getInstance(func);
81    }
82  0 return null;
83    }
84   
 
85  30 toggle private KeyDerivationFunctionFactory getFactory(String hint)
86    {
87  30 try {
88  30 return this.manager.getInstance(KeyDerivationFunctionFactory.class, hint);
89    } catch (ComponentLookupException e) {
90  0 throw new UnsupportedOperationException("Key derivation function algorithm not found.", e);
91    }
92    }
93    }