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

File DefaultSignerFactory.java

 

Coverage histogram

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

Code metrics

2
11
4
1
85
49
7
0.64
2.75
4
1.75

Classes

Class Line # Actions
DefaultSignerFactory 43 11 0% 7 7
0.588235358.8%
 

Contributing tests

This file is covered by 28 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.signer.internal.factory;
21   
22    import java.io.IOException;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26   
27    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
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.params.cipher.CipherParameters;
32    import org.xwiki.crypto.signer.Signer;
33    import org.xwiki.crypto.signer.SignerFactory;
34   
35    /**
36    * Default signer factory to be use for decoding existing signature algorithm identifier.
37    *
38    * @version $Id: 14b44db3f07a17b1cb3961c6e66d20d46565efce $
39    * @since 5.4RC1
40    */
41    @Component
42    @Singleton
 
43    public class DefaultSignerFactory extends AbstractSignerFactory implements BcSignerFactory
44    {
45    @Inject
46    private ComponentManager manager;
47   
 
48  0 toggle @Override
49    public org.xwiki.crypto.signer.Signer getInstance(boolean forSigning, CipherParameters parameters)
50    {
51  0 throw new UnsupportedOperationException("This signer does not support to be created from cipher parameters");
52    }
53   
 
54  7 toggle @Override
55    public Signer getInstance(boolean forSigning, CipherParameters parameters, byte[] encoded)
56    {
57  7 return getInstance(forSigning, parameters, AlgorithmIdentifier.getInstance(encoded));
58    }
59   
 
60  45 toggle @Override
61    public Signer getInstance(boolean forSigning, CipherParameters parameters, AlgorithmIdentifier algId)
62    {
63  45 SignerFactory factory = getFactory(algId.getAlgorithm().getId());
64   
65  45 if (factory instanceof BcSignerFactory) {
66  45 return ((BcSignerFactory) factory).getInstance(forSigning, parameters, algId);
67    }
68   
69  0 try {
70  0 return factory.getInstance(forSigning, parameters, algId.getEncoded());
71    } catch (IOException e) {
72    // Unlikely
73  0 throw new IllegalArgumentException("Unable to encode algorithm identifier.");
74    }
75    }
76   
 
77  45 toggle protected SignerFactory getFactory(String hint)
78    {
79  45 try {
80  45 return this.manager.getInstance(SignerFactory.class, hint);
81    } catch (ComponentLookupException e) {
82  0 throw new UnsupportedOperationException("Signing algorithm not found.", e);
83    }
84    }
85    }